15

I'm simulating the comet live feed protocol for my site, so in my controller I'm adding:

while(nothing_new && before_timeout){
  Thread.Sleep(1000);
}

But I noticed the whole website got slow after I added this feature. After debugging I concluded that when I call Thread.Sleep all the threads, even in other requests, are being blocked.

Why does Thread.Sleep block all threads, not just the current thread, and how should I deal with an issue like this?

Scath
  • 3,777
  • 10
  • 29
  • 40
Hilmi
  • 3,411
  • 6
  • 27
  • 55
  • 1
    What is the `cornet live feed protocol` and where can we find some documentation on that? – Mike Perrenoud Apr 02 '13 at 14:44
  • 2
    `Thread.Sleep` only blocks the thread it's called on. Something else must be going on. – George Duckett Apr 02 '13 at 14:45
  • 1
    Your conclusion does not logically follow from the evidence. – Eric Lippert Apr 02 '13 at 14:45
  • Depending how this code is being called every user on your website results in the thread being put to sleep. – Security Hound Apr 02 '13 at 14:46
  • Removed the comment. Strange results from Google, though. It showed this question twice with the same URL but showed one as visited and the other not visited... https://www.google.com/#hl=en&safe=off&sclient=psy-ab&q=%22comet+live+feed%22+protocol+&oq=%22comet+live+feed%22+protocol+&gs_l=hp.3...14714.18090.1.18679.2.2.0.0.0.0.89.174.2.2.0...0.0...1c.1.7.psy-ab.qktiITGz8vc&pbx=1&bav=on.2,or.r_cp.r_qf.&bvm=bv.44442042,d.eWU&fp=fe0e49d0245c2d37&biw=1600&bih=742 – Pete Apr 02 '13 at 14:48
  • @Pete It's because the title was changed, which results in the URL to the page changing, however the title in the URL isn't used by SO, it ignores it and just uses the question ID to render the page. – Servy Apr 02 '13 at 14:51

2 Answers2

28

What @Servy said is correct. In addition to his answer I would like to throw my 2 cents. I bet you are using ASP.NET Sessions and you are sending parallel requests from the same session (for example you are sending multiple AJAX requests). Except that the ASP.NET Session is not thread safe and you cannot have parallel requests from the same session. ASP.NET will simply serialize the calls and execute them sequentially.

That's why you are observing this blocking. It will block only requests from the same ASP.NET Session. If you send an HTTP requests from a different session it won't block. This behavior is by design and you can read more about it here.

ASP.NET Sessions are like a cancer and I recommend you disabling them as soon as you find out that they are being used in a web application:

<sessionState mode="Off" />

No more queuing. Now you've got a scalable application.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    that is correct! all of them are from the same session, all of them are ajax! – Hilmi Apr 02 '13 at 14:51
  • Yeap, I knew it. That's by design. – Darin Dimitrov Apr 02 '13 at 14:53
  • 1
    ok thanks alot sir, I dont know why everybody start to attack this question and votes down, but this is the answer i want ! thanks again sir, – Hilmi Apr 02 '13 at 14:54
  • Sir, I'll lose sessions as soon as i put their state to off, i even tried to add `lockItem="false"` and also didn't solve the issue, can i unlock it from the action or how can i deal with it? – Hilmi Apr 02 '13 at 16:55
  • 1
    thanks sir, i solved the issue by adding `[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]` on my controller.. thanks again – Hilmi Apr 02 '13 at 17:06
  • Small correction: the session state *is* thread safe - serializing multiple requests for the same session is how ASP.NET achieves thread safety. – Dan Parsonson Apr 11 '17 at 17:51
8

I concluded that when I call thread.sleep all the threads even in other requests are being blocked

That conclusion is incorrect. Thread.Sleep does not block any other thread, it only blocks the current thread. If multiple threads are all being blocked by this line of code then it is because all of those threads are hitting this line of code.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • sorry sir, my conclusion was correct, i seems like the asp blocks all thread with the same session. – Hilmi Apr 02 '13 at 15:12
  • 2
    @Hilmi No, your conclusion was not correct. `Thread.Sleep` doesn't block any other threads, it only blocks a single threads. All of the other threads were blocking because they were waiting on this thread, rather than because they were executing a sleep command. That's something entirely different. – Servy Apr 02 '13 at 15:16
  • indeed thats why i ask the question, dont you think? – Hilmi Apr 02 '13 at 16:32