1

Closed

Unhandled exception while using streaming API

description

Got this unhandled exception while receiving streaming tracks:

Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Net.WebException
Stack:
at LinqToTwitter.TwitterExecute+<>c__DisplayClass19.<GetBasicStreamRequest>b__14(System.IAsyncResult)
at System.Net.LazyAsyncResult.Complete(IntPtr)
at System.Net.ContextAwareResult.CaptureOrComplete(System.Threading.ExecutionContext ByRef, Boolean)
at System.Net.ContextAwareResult.FinishPostingAsyncOp()
at System.Net.HttpWebRequest.BeginGetRequestStream(System.AsyncCallback, System.Object)
at LinqToTwitter.TwitterExecute.GetBasicStreamRequest(LinqToTwitter.Request)
at LinqToTwitter.TwitterExecute.ExecuteTwitterStream(System.Object)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
Closed Jan 19, 2013 at 11:37 PM by JoeMayo

comments

JoeMayo wrote Jan 19, 2013 at 3:50 PM

Hi,

In most cases, LINQ to Twitter detects communications problems and performs automatic retries. However, in the case where Twitter explicitly disconnects the stream, LINQ to Twitter will throw a WebException. This is the notification to you that Twitter has disconnected. Here's what you should do:
  1. Write your code to catch a WebException.
  2. When you receive the WebException, create a new instance of TwitterContext. This is required - there's an issue with trying to reconnect with the same TwitterContext instance that I haven't resolved yet.
  3. Reconnect, but ensure you have a backoff strategy to avoid rapid reconnects. This gives Twitter time to recover from whatever problem they might have without getting hammered from all the clients reconnecting at one time.
If you haven't seen it yet, here's Twitter's Streaming API documentation. It contains a lot of design advice:

https://dev.twitter.com/docs/streaming-apis

Remember, that LINQ to Twitter is already doing some of the retry logic for you, which you might see in the log. However, for something fatal like an explicit reconnect, you might not want LINQ to Twitter doing everything for you. So, the current behavior is by design and gives you more flexibility in how to handle explicit disconnects by Twitter.

@JoeMayo

Sape wrote Jan 19, 2013 at 7:25 PM

Hi,

Thanks for the quick response. I have a windows service that basically just starts up a long running task which initiates, and once in a while updates, the streaming Track. The task does very little else except sleep while the streaming callbacks are fired. The exception it thrown very rarely but it caused the service to crash.
I noticed that I had not set up a UnhandledExceptionEventHandler to the AppDomain, and that most likely caused the crashes. I suppose adding the handler and reacting to the exception properly will solve my problem.

Thanks.