Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Does Socket.AcceptAsync throw SocketException for any transient reason?

Parent

Does Socket.AcceptAsync throw SocketException for any transient reason?

+0
−0

I'm writing exception handling around a call to Socket.AcceptAsync in a loop. One of the exceptions it's documented to throw is SocketException, but the documentation is vague:

An error occurred when attempting to access the socket.

I want to know if AcceptAsync throws SocketException for any transient reason. In particular, is there anything the remote client could do to cause it to throw? If I don't account for this, my program could be subject to denial of service attacks.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+0
−0

The source code for the Socket.AcceptAsync class seems to be this one.

The relevant code (the one related to exceptions being raised) is the following:

private bool AcceptAsync(SocketAsyncEventArgs e, CancellationToken cancellationToken)
{
	ThrowIfDisposed();

	ArgumentNullException.ThrowIfNull(e);
	if (e.HasMultipleBuffers)
	{
		throw new ArgumentException(SR.net_multibuffernotsupported, nameof(e));
	}
	if (_rightEndPoint == null)
	{
		throw new InvalidOperationException(SR.net_sockets_mustbind);
	}
	if (!_isListening)
	{
		throw new InvalidOperationException(SR.net_sockets_mustlisten);
	}

	SocketError socketError;
	try
	{
		socketError = e.DoOperationAccept(this, _handle, acceptHandle, cancellationToken);
	}
	catch (Exception ex)
	{
		SocketsTelemetry.Log.AfterAccept(SocketError.Interrupted, ex.Message);

		// Clear in-use flag on event args object.
		e.Complete();
		throw;
	}

	return socketError == SocketError.IOPending;
}

Based on this, I would consider handling the following exception types:

  • ObjectDisposedException
  • ArgumentNullException
  • InvalidOperationException
  • Exception (that is, even the library creators expect "other" exception types)
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Thanks for the code. The decompiler in VS was leading me in circles. But this doesn't quite resolve m... (1 comment)
Thanks for the code. The decompiler in VS was leading me in circles. But this doesn't quite resolve m...
Kevin Krumwiede‭ wrote 10 months ago

Thanks for the code. The decompiler in VS was leading me in circles. But this doesn't quite resolve my question. I think everything but the last catch and rethrow are what Eric Lippert called "boneheaded exceptions". If my socket isn't bound or listening, that's my mistake and I want the program to crash. But that last catch and rethrow may fall into Lippert's "exogenous" category. I'll try to dig into DoOperationAccept and see if I can find a definitive answer there.