IHttpAsyncHandler is hanging up after 3-4 requests

Last post 11-04-2008 12:30 AM by joshua_wss. 4 replies.

Sort Posts:

  • IHttpAsyncHandler is hanging up after 3-4 requests

    11-03-2008, 1:26 AM
    • Member
      point Member
    • joshua_wss
    • Member since 11-03-2008, 1:11 AM
    • Posts 3

     I am writing a ASP.NET based REAL TIME COMET server. But project is now 98% complete. But there is 1 mysterous thing I can not find anywhere.

    Actual code is quite long, so to reproduce the problem I wrote following simple handler.

     

        public class CometHandler : IHttpAsyncHandler
        {           
            #region IHttpAsyncHandler Members

            public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
            {     
                // TESTING
                AsyncResultWrapper ar = new AsyncResultWrapper(cb, extraData);

                if (context.Request.RawUrl.Contains("iframe")) {

                    // save this persitent connection dont close
                    context.Response.Write(CometServer.IFramePrologue);
                    context.Response.Write("<script>callback(-2,\"\")</script>");
                    context.Response.Flush();
                } else if (context.Request.RawUrl.Contains("connect")) {

                    // save this persitent connection dont close
                    context.Response.Write("<script>callback(-2,\"\")</script>");
                    context.Response.Flush();
                } else {
                    // close other connections
                    ar.SetCompleted();
                }
                //
              
                return ar;
               
            }

            public void EndProcessRequest(IAsyncResult result)
            {            
               
            }

            #endregion

            #region IHttpHandler Members

            public bool IsReusable
            {
                get { return false; }
            }

            public void ProcessRequest(HttpContext context)
            {
                throw new Exception("This method is never called by runtime.");
            }

            #endregion

            #region AsyncWrapper

            private class AsyncResultWrapper : IAsyncResult
            {
                private bool mIsCompleted;
                private AsyncCallback mCallBack;
                private object mState;

                public AsyncResultWrapper(AsyncCallback callback, object state)
                {
                    mIsCompleted = false;
                    mCallBack = callback;
                    mState = state;
                }

                #region IAsyncResult Members

                object IAsyncResult.AsyncState
                {
                    get { return mState; }
                }

                WaitHandle IAsyncResult.AsyncWaitHandle
                {
                    get { return null; }
                }

                bool IAsyncResult.CompletedSynchronously
                {
                    get { return false; }
                }

                bool IAsyncResult.IsCompleted
                {
                    get { return mIsCompleted; }
                }

                #endregion

                public void SetCompleted()
                {
                    mIsCompleted = true;
                    if (mCallBack != null) {
                        mCallBack(this);
                    }
                }
            }        

            #endregion
        }

    The problem is this:

    1. I connect to this handler via XMLHttpRequest object, this connection doesn't close for about 2 - 5 minute.

    2. I send short requests to this handler via XMLHttpRequest object

     

    I repeat this process on 1 firefox, 2 IE window. (total of 3 clients).

    But on the third client after I do step# 1, then when I send second request (step # 2) request doesn't come back. It hangs.

     

    I am assuming it is not on the client side, and also I didn't implement any of Session State interfaces.

    Please HELP ME, I will appreciate so MUCH!

  • Re: IHttpAsyncHandler is hanging up after 3-4 requests

    11-03-2008, 10:00 AM

    Is there an actual asynchronous operation that you're starting? Your code doesn't show it. In fact,the code, as posted, does not need to be async.

    John Saunders
  • Re: IHttpAsyncHandler is hanging up after 3-4 requests

    11-03-2008, 10:33 AM
    • Member
      point Member
    • joshua_wss
    • Member since 11-03-2008, 1:11 AM
    • Posts 3

    Yes, I have created custom THREAD POOL, and queueing the items there. So I was first wondering the problem was locking issues with my thread pool, so I removed it and checked. It was ok. 

     

    This code i posted is not my real Comet Server application code. They are not relevant pieces, I checked. What is important is when new request comes in I somehow keeps or saves some connections in list and close some connections immediately.

     

    I know I compacted the code, IT IS JUST A TEST CASE. It doesn't have to include real ASYNC operation. 

     

    And also, today I found some strange thing, 

    Is it ok to call HttpContext's Resonse.Write on any thread, or I should accumulate all data in async thread and DO THE WRITE operation in Async handler's EndResponse callback method?

     

    And also I am using 2 XMLHttpRequest object on the client side. 

    1. First one connects to this async handler like /connect/Comet.ashx  etc .. So the server will not immediately close this connection but keep it

    2. I send short requests on the other one, so server will close it immediately after processing.

     

    My platform is: IIS 7, Vista Home Premium, and also tested on Windows XP PRO and IIS 6.

    All the same, third clients 2ND XMLHttpRequest hangs after the first one. I am really wondering why.

     

    Is this Write, Flush operations causing locks, I dont know, It is really hard to find for me. PLEASE HELP, again

     

    I need to finish this very soon,

    If you need any other information, please let me know.

  • Re: IHttpAsyncHandler is hanging up after 3-4 requests

    11-03-2008, 1:21 PM

    If you didn't know it before, then you certainly know it now: multithreaded programming is not easy.

    I doubt that anyone could help you on this without seeing all of the code. Certainly, I will not have time to help you, even if you post the code - like I said, this is not easy stuff.

    One little bit of advice I'd give is to make sure that the ThreadStart method, or any other async code, is surrounded by a try/catch block. Otherwise, an unhandled exception will quietly terminate the thread, and you'll have no idea what happened. Make sure you catch the exception, log ex.ToString() somewhere thread-safe (like the Windows event log), then rethrow the exception, since you're not actually handling it.

    Oh, and read the documentation on HttpRequest.Write to learn if it is thread-safe. Certainly, I wouldn't want to have multiple threads calling it without synchronization, though the synchronization may already be there.

    John Saunders
  • Re: IHttpAsyncHandler is hanging up after 3-4 requests

    11-04-2008, 12:30 AM
    • Member
      point Member
    • joshua_wss
    • Member since 11-03-2008, 1:11 AM
    • Posts 3

     As you see the example code, there is no CUSTOM MULTITHREADING, I intentionally removed them all.

     

    Problem is , you see how simple is the CODE ABOVE, but IT DOESN"T WORK the way I want it to.

    There is no locking on i am making on any objects, it must work, but it doesn't

Page 1 of 1 (5 items)