Skip to content

Commit ad31812

Browse files
aamCommit Queue
authored andcommitted
[io] Fix race between closing socket and requesting its type.
Fixes #61582 Revert "[io/socket] Make Socket::fd_ atomic since it can get updated from multiple threads." Revert "[gardening] Fix fuchsia build - assign atomic to intptr_t before reinterpret_cast to pointer." TEST=ci Change-Id: Ia755d6623e46b7940800a12bf4a5d8d9cb674522 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/451822 Reviewed-by: Slava Egorov <[email protected]> Commit-Queue: Alexander Aprelev <[email protected]>
1 parent a4016eb commit ad31812

File tree

6 files changed

+17
-13
lines changed

6 files changed

+17
-13
lines changed

runtime/bin/socket.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,11 @@ void FUNCTION_NAME(Socket_GetFD)(Dart_NativeArguments args) {
953953
void FUNCTION_NAME(Socket_GetType)(Dart_NativeArguments args) {
954954
Socket* socket =
955955
Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0));
956+
if (socket->fd() < 0) {
957+
OSError os_error(-1, "Socket is closed.", OSError::kUnknown);
958+
Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error));
959+
return;
960+
}
956961
OSError os_error;
957962
intptr_t type = SocketBase::GetType(socket->fd());
958963
if (type >= 0) {

runtime/bin/socket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class Socket : public ReferenceCounted<Socket> {
124124
static bool short_socket_read_;
125125
static bool short_socket_write_;
126126

127-
std::atomic<intptr_t> fd_;
127+
intptr_t fd_;
128128
Dart_Port isolate_port_;
129129
Dart_Port port_;
130130
uint8_t* udp_receive_buffer_;

runtime/bin/socket_base_win.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,6 @@ void SocketBase::GetError(intptr_t fd, OSError* os_error) {
215215
}
216216

217217
int SocketBase::GetType(intptr_t fd) {
218-
if (fd < 0) {
219-
return -1;
220-
}
221-
222218
Handle* handle = reinterpret_cast<Handle*>(fd);
223219
switch (GetFileType(handle->handle())) {
224220
case FILE_TYPE_CHAR:

runtime/bin/socket_fuchsia.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ void Socket::SetClosedFd() {
5555
}
5656

5757
void Socket::CloseFd() {
58-
intptr_t fd_handle = fd();
59-
ASSERT(fd_handle != kClosedFd);
60-
IOHandle* handle = reinterpret_cast<IOHandle*>(fd_handle);
58+
ASSERT(fd_ != kClosedFd);
59+
IOHandle* handle = reinterpret_cast<IOHandle*>(fd_);
6160
ASSERT(handle != nullptr);
6261
handle->Release();
6362
SetClosedFd();

runtime/bin/socket_win.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ Socket::Socket(intptr_t fd)
2626
port_(ILLEGAL_PORT),
2727
udp_receive_buffer_(nullptr) {
2828
ASSERT(fd_ != kClosedFd);
29-
intptr_t fd_handle = fd_;
30-
Handle* handle = reinterpret_cast<Handle*>(fd_handle);
29+
Handle* handle = reinterpret_cast<Handle*>(fd_);
3130
ASSERT(handle != nullptr);
3231
}
3332

3433
void Socket::CloseFd() {
35-
intptr_t fd_handle = fd();
36-
ASSERT(fd_handle != kClosedFd);
37-
Handle* handle = reinterpret_cast<Handle*>(fd_handle);
34+
ASSERT(fd_ != kClosedFd);
35+
Handle* handle = reinterpret_cast<Handle*>(fd_);
3836
ASSERT(handle != nullptr);
3937
handle->Release();
4038
SetClosedFd();

sdk/lib/_internal/vm/bin/stdio_patch.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ class _StdIOUtils {
4848
}
4949

5050
static int _nativeSocketType(_NativeSocket nativeSocket) {
51+
if (nativeSocket.isClosed) {
52+
throw FileSystemException("Socket is closed");
53+
}
54+
if (nativeSocket.isClosing) {
55+
throw FileSystemException("Socket is being closed");
56+
}
5157
var result = _getSocketType(nativeSocket);
5258
if (result is OSError) {
5359
throw FileSystemException("Error retrieving socket type", "", result);

0 commit comments

Comments
 (0)