[io] Fix race between closing socket and requesting its type.

Fixes https://github.com/dart-lang/sdk/issues/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 <vegorov@google.com>
Commit-Queue: Alexander Aprelev <aam@google.com>
This commit is contained in:
Alexander Aprelev
2025-10-01 15:15:20 -07:00
committed by Commit Queue
parent a4016ebd7d
commit ad318128f5
6 changed files with 17 additions and 13 deletions
+5
View File
@@ -953,6 +953,11 @@ void FUNCTION_NAME(Socket_GetFD)(Dart_NativeArguments args) {
void FUNCTION_NAME(Socket_GetType)(Dart_NativeArguments args) {
Socket* socket =
Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0));
if (socket->fd() < 0) {
OSError os_error(-1, "Socket is closed.", OSError::kUnknown);
Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error));
return;
}
OSError os_error;
intptr_t type = SocketBase::GetType(socket->fd());
if (type >= 0) {
+1 -1
View File
@@ -124,7 +124,7 @@ class Socket : public ReferenceCounted<Socket> {
static bool short_socket_read_;
static bool short_socket_write_;
std::atomic<intptr_t> fd_;
intptr_t fd_;
Dart_Port isolate_port_;
Dart_Port port_;
uint8_t* udp_receive_buffer_;
-4
View File
@@ -215,10 +215,6 @@ void SocketBase::GetError(intptr_t fd, OSError* os_error) {
}
int SocketBase::GetType(intptr_t fd) {
if (fd < 0) {
return -1;
}
Handle* handle = reinterpret_cast<Handle*>(fd);
switch (GetFileType(handle->handle())) {
case FILE_TYPE_CHAR:
+2 -3
View File
@@ -55,9 +55,8 @@ void Socket::SetClosedFd() {
}
void Socket::CloseFd() {
intptr_t fd_handle = fd();
ASSERT(fd_handle != kClosedFd);
IOHandle* handle = reinterpret_cast<IOHandle*>(fd_handle);
ASSERT(fd_ != kClosedFd);
IOHandle* handle = reinterpret_cast<IOHandle*>(fd_);
ASSERT(handle != nullptr);
handle->Release();
SetClosedFd();
+3 -5
View File
@@ -26,15 +26,13 @@ Socket::Socket(intptr_t fd)
port_(ILLEGAL_PORT),
udp_receive_buffer_(nullptr) {
ASSERT(fd_ != kClosedFd);
intptr_t fd_handle = fd_;
Handle* handle = reinterpret_cast<Handle*>(fd_handle);
Handle* handle = reinterpret_cast<Handle*>(fd_);
ASSERT(handle != nullptr);
}
void Socket::CloseFd() {
intptr_t fd_handle = fd();
ASSERT(fd_handle != kClosedFd);
Handle* handle = reinterpret_cast<Handle*>(fd_handle);
ASSERT(fd_ != kClosedFd);
Handle* handle = reinterpret_cast<Handle*>(fd_);
ASSERT(handle != nullptr);
handle->Release();
SetClosedFd();
@@ -48,6 +48,12 @@ class _StdIOUtils {
}
static int _nativeSocketType(_NativeSocket nativeSocket) {
if (nativeSocket.isClosed) {
throw FileSystemException("Socket is closed");
}
if (nativeSocket.isClosing) {
throw FileSystemException("Socket is being closed");
}
var result = _getSocketType(nativeSocket);
if (result is OSError) {
throw FileSystemException("Error retrieving socket type", "", result);