2

My company has an application that keeps track of information related to web sites that are hosted on various machines. A central server runs a windows service that gets a list of sites to check, and then queries a service running on those target sites to get a response that can be used to update the local data.

My task has been to apply multithreading to this process to reduce the time it takes to run through all the sites (almost 3000 sites that take about 8 hours to run sequentially). The service runs through successfuly when it's not multithreaded, but the moment I spread out the work to multiple threads (testing with 3 right now, plus a watcher thread) there's a bizarre crash that seems to originate from the call to the remote services that are supposed to provide the data. It's a SOAP/XML call.

When run on the test server, the service just gives up and doesn't complete it's task, but doesn't stop running. When run through the debugger (Dev Studio 2010) the whole thing just stops. I'll run it, and seconds later it'll stop debugging, but not because it completed. It does not throw an exception or give me any kind of message. With breakpoints I can walk through to the point where it just stops. Event logging leads me to the same spot. It stops on the line of code that tries to get a response from the web service on the other sites. And again: it only does that when multithreaded.

I found some information that suggested there's a limit to the number of connections that defaults to 2. The proposed solution is to add some tags to the app.config, but that hasn't solved the problem...

<system.net>
    <connectionManagement>
        <add address="*" maxconnection="20"/>
    </connectionManagement>
</system.net>

I still think it might be related to the number of allowed connections, but I have been unable to find information around it online very well. Is there something straightforward I'm missing? Any help would be much appreciated.

Kevin
  • 175
  • 8
  • If you limit it to just two threads, do you still get the application stopping? If so, then it isn't the two connection limit. At least that way you can eliminate that aspect, or not. – Colin Mackay Jul 01 '11 at 14:10
  • It didn't occur to me until it was pointed out to me, but I did try running one thread days ago. I feel a bit stupid for missing it. And yes... the error still happens when I run one thread in a "threadpool", but not when I run the process all in the main thread. So I suppose my question is different: Why would a service call kill the process when run through a new thread, and not when run in the main thread? – Kevin Jul 01 '11 at 16:18
  • Perhaps you can post some more code so we can more closely examine it. – Bryan Crosby Jul 15 '11 at 16:13
  • 2
    a stab in the dark: i've seen a similar effect while working with a stock market trading API ... that little beast had the restriction that it may only be operated from one thread ... it worked from more than one thread most the time, but if there was an exception from that lib on another thread than the main UI thread, visual studio was unable to catch it... the debugger simply died ... try surrounding the suspicious lines with a try-catch and set a breakpoint in the catch block ... – DarkSquirrel42 Jul 18 '11 at 00:39
  • #DarkSquirrel42: You should be getting some kind of exception, yeah? Check the stacktrace, inner exception tree, etc. – Jake Berger Aug 08 '11 at 17:25

1 Answers1

0
  1. No crash however bizarre will escape the stack-dump. Try going through that dump and see if it points out to some obvious function.
  2. Are you using some third party tool or some other component for the actual service call ? If yes, then please check the documentation/contact-the-person-who-wrote-it, to confirm that their components are thread safe. If they are not, you have large task ahead. :) (I have worked on DB which are not safe, so trust me it is not very uncommon to find few global static variables thrown around..)

Lastly if you are 100% sure that this is due multiple threads then, put a lock in your worked thread. Initially say it covers entire main-while-loop. Therotically it should not crash not as even though it is multi-threaded, you have serialized the execution. Next step is to reduce to scope of the thread. Say, there are three functions in the main-while-loop , say f1(), f2(), f3(), then start locking f2() and f3() while leaving f1 unlocked... If things work out, then problem is somewhere in f2 or f3(). I hope you got the idea of what I am suggest

I know this is like blind man guessing elephant, but that is the best you can do, if your code uses LOT many external component which are not adequately documented.

Ajeet Ganga
  • 8,353
  • 10
  • 56
  • 79
  • Re #1: Unless of course you hit a bizarre Stack Overflow. A `StackOverflowException` will not give you a stack. – vcsjones Aug 27 '11 at 22:08
  • Point taken. :) I am not sure what would happen in such case. May be it will still show last few frames ? Also I assume, kevin's issue has something to do with thread-unsafe components rather than some infinite loop. – Ajeet Ganga Aug 27 '11 at 22:23