-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to set a timeout on snmp requests #23
Comments
Here you need to follow the general .NET pattern, |
Does this also close the connection related to the request? |
SNMP/UDP has no connection at all. If you want to know "whether the resources are freed when timeout occurs", then I currently has no accurate answer. |
The .NET async design link mentioned above, and plenty of other examples on StackOverflow, all point to using things like Task.WhenAny to create timeouts for async tasks. So you can do this:
This looks great - if there is no response to the GetAsync call within 2000ms, the Delay task completes and the timeout logic is run. However the GetAsync call is still alive, there is a socket listening for the response, but it is not possible to (nicely) stop it. You can call task.Dispose() in the timeout handler but that raises an exception on the task because you're not able to dispose a task that is running. You can see the tasks all queueing up if you look at the application's memory usage. The un-answered ('timed out') calls gum up the stack with large amounts of pinned memory waiting to receive the repsonses that will never come. It's a huge resource leak and makes the Async implementation here fairly useless unless you know you're always going to get a response. The solution is for the API's Async methods to accept a CancellationToken which allow the task to be signalled to terminate early - this would be signalled from the timeout handler. Until that happens I must continue to use the synchronous methods. (On that note, why mark them as 'Obsolete' when they still work just fine and in many cases, like here, they are still very much the only way to use the API?) Thanks |
The I won't comment much on the "CancellationToken" part. Whether Microsoft would introduce that in socket API surface is unknown. |
Cheers - it absolutely baffles me that Microsoft omit what seems to be an essential part of the async mechanism from their socket API. Thanks |
I've already dealt with that in my project OltSnmp.cs and it seems to be working fine with .NET Core on both Windows and Linux. |
For future reference, the latest 9.0.6 Beta 1 release added .NET Standard 1.3 support, but in the meantime kept most sync API. |
How can I set a timeout on snmp requests like SnmpGetAsync or SnmpWalkAsync in order not to wait indefinitely for result? I would like to get an exception for instance when remote host in unavailable for some period of time.
The text was updated successfully, but these errors were encountered: