|
| 1 | +// Copyright (c) 2025, 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 | +// Verifies that restricted isolate can't be terminated. |
| 6 | +// |
| 7 | +import 'dart:isolate'; |
| 8 | + |
| 9 | +import 'package:expect/async_helper.dart'; |
| 10 | +import 'package:expect/expect.dart'; |
| 11 | + |
| 12 | +main() async { |
| 13 | + asyncStart(); |
| 14 | + |
| 15 | + int receivedCounter = -1; |
| 16 | + final rp = RawReceivePort((value) { |
| 17 | + receivedCounter = value; |
| 18 | + }); |
| 19 | + final rpExit = ReceivePort(); |
| 20 | + |
| 21 | + final isolate = await Isolate.spawn( |
| 22 | + (sendPort) async { |
| 23 | + int counter = 0; |
| 24 | + while (true) { |
| 25 | + sendPort.send(counter++); |
| 26 | + await Future.delayed(Duration(milliseconds: 100)); |
| 27 | + } |
| 28 | + }, |
| 29 | + rp.sendPort, |
| 30 | + onExit: rpExit.sendPort, |
| 31 | + ); |
| 32 | + |
| 33 | + // Wait for the isolate to start. |
| 34 | + while (receivedCounter < 0) { |
| 35 | + await Future.delayed(Duration(milliseconds: 100)); |
| 36 | + } |
| 37 | + |
| 38 | + // Create restricted isolate, the one that can't be terminated. |
| 39 | + final restricted = Isolate(isolate.controlPort); |
| 40 | + restricted.kill(); |
| 41 | + final before_kill = receivedCounter; |
| 42 | + // Wait couple cycles to ensure isolate is still alive. |
| 43 | + while (receivedCounter < before_kill + 2) { |
| 44 | + await Future.delayed(Duration(milliseconds: 100)); |
| 45 | + } |
| 46 | + |
| 47 | + // Now kill the original isolate and wait for it to exit. |
| 48 | + isolate.kill(); |
| 49 | + await rpExit.first; |
| 50 | + |
| 51 | + rp.close(); |
| 52 | + asyncEnd(); |
| 53 | +} |
0 commit comments