Skip to content

Commit 322baef

Browse files
rmacnak-googleCommit Queue
authored and
Commit Queue
committed
[io] Exit the isolate during Process.runSync and sleep.
This prevents such an isolate from occupying one of the limited number of mutator slots and blocking other isolates in the same group from running. TEST=ci Bug: #51254 Bug: #54687 Bug: #57119 Change-Id: Ic04bbaa7f482d533ad0ecf2c6da17ea9f00c264e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/398927 Reviewed-by: Alexander Aprelev <[email protected]> Commit-Queue: Ryan Macnak <[email protected]>
1 parent 6f992ae commit 322baef

7 files changed

+72
-0
lines changed

runtime/bin/dartutils.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,20 @@ class ScopedBlockingCall {
651651
DISALLOW_COPY_AND_ASSIGN(ScopedBlockingCall);
652652
};
653653

654+
// Remove once we remove the limitation on the number of running mutators.
655+
// https://github.com/dart-lang/sdk/issues/54687
656+
class LeaveIsolateScope {
657+
public:
658+
LeaveIsolateScope() : isolate_(Dart_CurrentIsolate()) { Dart_ExitIsolate(); }
659+
~LeaveIsolateScope() { Dart_EnterIsolate(isolate_); }
660+
661+
private:
662+
Dart_Isolate isolate_;
663+
664+
DISALLOW_ALLOCATION();
665+
DISALLOW_COPY_AND_ASSIGN(LeaveIsolateScope);
666+
};
667+
654668
struct MagicNumberData {
655669
static constexpr intptr_t kMaxLength = 8;
656670

runtime/bin/process.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ void FUNCTION_NAME(Process_Sleep)(Dart_NativeArguments args) {
285285
int64_t milliseconds = 0;
286286
// Ignore result if passing invalid argument and just set exit code to 0.
287287
DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 0), &milliseconds);
288+
289+
LeaveIsolateScope leave_isolate;
288290
TimerUtils::Sleep(milliseconds);
289291
}
290292

runtime/bin/process_linux.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,8 @@ bool Process::Wait(intptr_t pid,
849849

850850
int alive = 3;
851851
while (alive > 0) {
852+
LeaveIsolateScope leave_isolate;
853+
852854
// Blocking call waiting for events from the child process.
853855
if (TEMP_FAILURE_RETRY(poll(fds, alive, -1)) <= 0) {
854856
return CloseProcessBuffers(fds, alive);

runtime/bin/process_macos.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,8 @@ bool Process::Wait(intptr_t pid,
826826

827827
int alive = 3;
828828
while (alive > 0) {
829+
LeaveIsolateScope leave_isolate;
830+
829831
// Blocking call waiting for events from the child process.
830832
if (TEMP_FAILURE_RETRY(poll(fds, alive, -1)) <= 0) {
831833
return CloseProcessBuffers(fds, alive);

runtime/bin/process_win.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,8 @@ bool Process::Wait(intptr_t pid,
828828
// Continue until all handles are closed.
829829
int alive = kHandles;
830830
while (alive > 0) {
831+
LeaveIsolateScope leave_isolate;
832+
831833
// Blocking call waiting for events from the child process.
832834
DWORD wait_result = WaitForMultipleObjects(alive, events, FALSE, INFINITE);
833835

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import "dart:isolate";
6+
import "dart:io";
7+
8+
child(replyPort) {
9+
replyPort.send(null);
10+
Process.runSync("sleep", ["3600"]);
11+
}
12+
13+
main() async {
14+
var pending = 0;
15+
var port;
16+
port = new RawReceivePort((msg) {
17+
pending--;
18+
if (pending == 0) exit(0);
19+
});
20+
21+
for (var i = 0; i < 20; i++) {
22+
Isolate.spawn(child, port.sendPort);
23+
pending++;
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import "dart:isolate";
6+
import "dart:io";
7+
8+
child(replyPort) {
9+
replyPort.send(null);
10+
sleep(Duration(minutes: 60));
11+
}
12+
13+
main() async {
14+
var pending = 0;
15+
var port;
16+
port = new RawReceivePort((msg) {
17+
pending--;
18+
if (pending == 0) exit(0);
19+
});
20+
21+
for (var i = 0; i < 20; i++) {
22+
Isolate.spawn(child, port.sendPort);
23+
pending++;
24+
}
25+
}

0 commit comments

Comments
 (0)