3
3
// https://opensource.org/licenses/MIT.
4
4
5
5
import 'dart:async' ;
6
- import 'dart:ffi' ;
7
- import 'dart:io' ;
8
- import 'dart:isolate' ;
9
6
import 'dart:typed_data' ;
10
7
11
- import 'package:native_synchronization/mailbox.dart' ;
12
8
import 'package:pool/pool.dart' ;
13
9
import 'package:protobuf/protobuf.dart' ;
14
10
import 'package:stream_channel/stream_channel.dart' ;
15
11
16
- import 'compilation_dispatcher .dart' ;
12
+ import 'concurrency.dart' if (dart.library.js) 'js/concurrency .dart' ;
17
13
import 'embedded_sass.pb.dart' ;
18
- import 'reusable_isolate.dart' ;
14
+ import 'isolate_main.dart' ;
15
+ import 'reusable_isolate.dart' if (dart.library.js) 'js/reusable_isolate.dart' ;
19
16
import 'util/proto_extensions.dart' ;
20
17
import 'utils.dart' ;
21
18
@@ -25,6 +22,9 @@ class IsolateDispatcher {
25
22
/// The channel of encoded protocol buffers, connected to the host.
26
23
final StreamChannel <Uint8List > _channel;
27
24
25
+ /// The callback which is called when fatal error occurs.
26
+ final void Function () _onError;
27
+
28
28
/// All isolates that have been spawned to dispatch to.
29
29
///
30
30
/// Only used for cleaning up the process when the underlying channel closes.
@@ -38,16 +38,12 @@ class IsolateDispatcher {
38
38
39
39
/// A pool controlling how many isolates (and thus concurrent compilations)
40
40
/// may be live at once.
41
- ///
42
- /// More than MaxMutatorThreadCount isolates in the same isolate group
43
- /// can deadlock the Dart VM.
44
- /// See https://github.com/sass/dart-sass/pull/2019
45
- final _isolatePool = Pool (sizeOf <IntPtr >() <= 4 ? 7 : 15 );
41
+ final _isolatePool = Pool (concurrencyLimit);
46
42
47
43
/// Whether [_channel] has been closed or not.
48
44
var _closed = false ;
49
45
50
- IsolateDispatcher (this ._channel);
46
+ IsolateDispatcher (this ._channel, this ._onError );
51
47
52
48
void listen () {
53
49
_channel.stream.listen ((packet) async {
@@ -112,7 +108,7 @@ class IsolateDispatcher {
112
108
isolate = _inactiveIsolates.first;
113
109
_inactiveIsolates.remove (isolate);
114
110
} else {
115
- var future = ReusableIsolate .spawn (_isolateMain ,
111
+ var future = ReusableIsolate .spawn (isolateMain ,
116
112
onError: (Object error, StackTrace stackTrace) {
117
113
_handleError (error, stackTrace);
118
114
});
@@ -144,7 +140,7 @@ class IsolateDispatcher {
144
140
_channel.sink.add (packet);
145
141
case 2 :
146
142
_channel.sink.add (packet);
147
- exit (exitCode );
143
+ _onError ( );
148
144
}
149
145
});
150
146
@@ -168,7 +164,7 @@ class IsolateDispatcher {
168
164
{int ? compilationId, int ? messageId}) {
169
165
sendError (compilationId ?? errorId,
170
166
handleError (error, stackTrace, messageId: messageId));
171
- _channel.sink. close ();
167
+ _onError ();
172
168
}
173
169
174
170
/// Sends [message] to the host.
@@ -179,7 +175,3 @@ class IsolateDispatcher {
179
175
void sendError (int compilationId, ProtocolError error) =>
180
176
_send (compilationId, OutboundMessage ()..error = error);
181
177
}
182
-
183
- void _isolateMain (Mailbox mailbox, SendPort sendPort) {
184
- CompilationDispatcher (mailbox, sendPort).listen ();
185
- }
0 commit comments