开发者

Thread-safe network stream freezes the thread who call it's Close() method

开发者 https://www.devze.com 2023-02-03 14:52 出处:网络
A thread-safe stream from WebResponse.GetResponseStream() is passed to another thread which will read the stream to the end and Close() it. stream.synchronized() is used for stream thread safety:

A thread-safe stream from WebResponse.GetResponseStream() is passed to another thread which will read the stream to the end and Close() it. stream.synchronized() is used for stream thread safety:

// Thread A
HttpWebResponse response = request.GetResponse(); 
return Stream.Synchronized(response.GetResponseStream());

// Thread B
// Read the stream to the end
stream.Close(); // Here thread doesn't get b开发者_C百科ack

Thread B is under control of another thread which is not the subject of this question (thread C). Only thing about thread C is if thread B doesn't exit properly it will Abort() thread B. The problem is Thread B freezes at stream.Close() and it's always aborted by thread C. Whatever the timeout (a waithandle with timeout is used) I set, thread B will not pass that line and during the timeout period CPU is 100%

What I'm doing wrong ?


Suggest starting with something like TCP/View to find the state of the underlying stream—is the other end not allowing it to clean up cleanly? Or do you need to set socket options to close more quickly?

A workaround might be to allow the Close to happen in the thread pool, or allow the GC to put the unclosed stream into the finalisation queue.


Try to call request.Abort() before stream.Close() if nothing else helping you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号