Skip to content

[io/http] Fix race condition where RawSecureSocket dispatches read events after shutdown#62520

Open
Colton127 wants to merge 2 commits intodart-lang:mainfrom
Colton127:fix-62514
Open

[io/http] Fix race condition where RawSecureSocket dispatches read events after shutdown#62520
Colton127 wants to merge 2 commits intodart-lang:mainfrom
Colton127:fix-62514

Conversation

@Colton127
Copy link

This PR resolves an issue where RawSecureSocket potentially emits a RawSocketEvent.read event after the read-side of the socket has been closed. The receiver would then attempt to read from the socket, which throws SocketException: Reading from a closed socket.

Issue: #62514


  • I’ve reviewed the contributor guide and applied the relevant portions to this PR.
Contribution guidelines:
  • See our contributor guide for general expectations for PRs.
  • Larger or significant changes should be discussed in an issue before creating a PR.
  • Contributions to our repos should follow the Dart style guide and use dart format.

Note that this repository uses Gerrit for code reviews. Your pull request will be automatically converted into a Gerrit CL and a link to the CL written into this PR. The review will happen on Gerrit but you can also push additional commits to this PR to update the code review.

@copybara-service
Copy link

Thank you for your contribution! This project uses Gerrit for code reviews. Your pull request has automatically been converted into a code review at:

https://dart-review.googlesource.com/c/sdk/+/475941

Please wait for a developer to review your code review at the above link; you can speed up the review if you sign into Gerrit and manually add a reviewer that has recently worked on the relevant code. See CONTRIBUTING.md to learn how to upload changes to Gerrit directly.

Additional commits pushed to this PR will update both the PR and the corresponding Gerrit CL. After the review is complete on the CL, your reviewer will merge the CL (automatically closing this PR).

@mraleph
Copy link
Member

mraleph commented Jan 29, 2026

@Colton127 could you include this as a regression test (you can place it in tests/standalone/io/regress_62514_test.dart):

// Copyright (c) 2026, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
//
// OtherResources=certificates/server_chain.pem
// OtherResources=certificates/server_key.pem

// Test that cancellation of subscription to [SecureSocket] works as
// expected.

import 'dart:async';
import 'dart:io';
import 'dart:typed_data';

import 'package:async_helper/async_helper.dart';
import 'package:expect/expect.dart';

String relativeToScript(path) => Platform.script.resolve(path).toFilePath();

final String serverCert = relativeToScript('certificates/server_chain.pem');
final String serverKey = relativeToScript('certificates/server_key.pem');

void main() {
  asyncTest(() async {
    final serverContext = SecurityContext()
      ..useCertificateChain(serverCert)
      ..usePrivateKey(serverKey, password: 'dartdart');

    final secureServer = await SecureServerSocket.bind(
      InternetAddress.loopbackIPv4,
      0,
      serverContext,
    );

    final dataStream = Stream.fromIterable(
      Iterable<List<int>>.generate(100, (_) => Uint8List(1024 * 1024)),
    );

    secureServer.listen((client) async {
      await dataStream.pipe(client);
      await client.close();
    });

    final secureClient = await SecureSocket.connect(
      InternetAddress.loopbackIPv4,
      secureServer.port,
      context: SecurityContext()..setTrustedCertificates(serverCert),
    );
    late final StreamSubscription clientSub;
    clientSub = secureClient.listen((event) async {
      clientSub.cancel();
    });
    await clientSub.asFuture();

    await Future.wait([secureClient.close(), secureServer.close()]);
  });
}

@Colton127
Copy link
Author

@mraleph Got it, thank you

@copybara-service
Copy link

https://dart-review.googlesource.com/c/sdk/+/475941 has been updated with the latest commits from this pull request.

1 similar comment
@copybara-service
Copy link

https://dart-review.googlesource.com/c/sdk/+/475941 has been updated with the latest commits from this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants