Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,14 @@ private void SendFileInternal(string? fileName, ReadOnlySpan<byte> preBuffer, Re
Send(preBuffer);
}

// Send the file, if any
if (fileHandle != null)
using (fileHandle)
Copy link
Member

@stephentoub stephentoub Aug 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be done earlier. The preBuffer sending above should be a part of this so that the fileHandle will be disposed if that Send throws.

{
// This can throw ObjectDisposedException.
errorCode = SocketPal.SendFile(_handle, fileHandle);
// Send the file, if any
if (fileHandle != null)
{
// This can throw ObjectDisposedException.
errorCode = SocketPal.SendFile(_handle, fileHandle);
}
}

if (errorCode != SocketError.Success)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,13 @@ private void SendFileInternal(string? fileName, ReadOnlySpan<byte> preBuffer, Re
{
// Open the file, if any
SafeFileHandle? fileHandle = OpenFileHandle(fileName);

SocketError errorCode;
// This can throw ObjectDisposedException.
errorCode = SocketPal.SendFile(_handle, fileHandle, preBuffer, postBuffer, flags);

using (fileHandle)
{
// This can throw ObjectDisposedException.
errorCode = SocketPal.SendFile(_handle, fileHandle, preBuffer, postBuffer, flags);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be:

SocketError errorCode;
using (SafeFileHandle? fileHandle = OpenFileHandle(fileName))
{
    // This can throw ObjectDisposedException.
    errorCode = SocketPal.SendFile(_handle, fileHandle, preBuffer, postBuffer, flags);
}


if (errorCode != SocketError.Success)
{
Expand Down