-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Socket.Select: increase ref count while the handle is in use #41763
Conversation
why do we need this @tmds? Should SafeHandle be sufficient to keep it alive? |
src/System.Net.Sockets/src/System/Net/Sockets/Socket.Windows.cs
Outdated
Show resolved
Hide resolved
src/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Unix.cs
Outdated
Show resolved
Hide resolved
{ | ||
throw new ArgumentException(SR.Format(SR.net_sockets_select, socketList[current].GetType().FullName, typeof(System.Net.Sockets.Socket).FullName), nameof(socketList)); | ||
} | ||
|
||
fileDescriptorSet[current + 1] = ((Socket)socketList[current])._handle.DangerousGetHandle(); | ||
bool success = false; | ||
socket.SafeHandle.DangerousAddRef(ref success); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With your other PR, accessing socket.SafeHandle is now going to mark the handle as exposed. Is that necessary? Should we instead be using _handle here and everywhere else internally where the handle is used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just added an InternalSafeHandle
property to the other PR. Once it is merged, I'll rebase and use it here.
The problem is the implementation isn't just passing SafeHandle to P/Invokes, where the marshaling layer would implicitly addref and release around the native call. Rather, the implementation is calling DangerousGetHandle, returning the raw pointer to then be used. Any usage of that raw handle is no longer guarded by the SafeHandle; instead, the SafeHandle needs to have its ref count increased prior to any usage of the raw handle and its ref count then decremented after. Otherwise, Dispose'ing of the SafeHandle could cause the handle to be closed and even recycled while it's still in use. |
I was under impression that was fixed in 3.0. If not, shoud we pass SafeHandle to the call? |
We need to be able to pass a variable number of handles / file descriptors in an array; we can't do that with SafeHandle.
What specifically? |
For the operations that take a single handle, the raw values were replaced by |
72c6bbd
to
f993aab
Compare
f993aab
to
328d455
Compare
The failures here (at least some of them) appear to be related hangs:
|
/azp run corefx-ci |
Azure Pipelines successfully started running 1 pipeline(s). |
On Windows, there were missing unrefs. This is fixed and ci is now passing. |
/azp run |
Azure Pipelines successfully started running 4 pipeline(s). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
…corefx#41763) * Socket.Select: increase ref count while the handle is in use * PR feedback * Use Socket.InternalSafeHandle for ref/release. * Socket.Windows: SelectFileDescriptor: add missing unrefs Commit migrated from dotnet/corefx@6c62c7f
CC @stephentoub