Skip to content

Commit 5847893

Browse files
committed
cache
1 parent ff1ddbd commit 5847893

4 files changed

Lines changed: 89 additions & 2 deletions

File tree

lib/core/desktop/helper_client.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,19 @@ final class HelperCoreLease implements CoreProcessLease {
358358

359359
@override
360360
Future<CoreProcessStopResult> stop(Duration timeout) {
361-
return _stopOperation ??= _stop();
361+
final stopOperation = _stopOperation;
362+
if (stopOperation != null) {
363+
return stopOperation;
364+
}
365+
final nextOperation = _stop().onError((
366+
Object error,
367+
StackTrace stackTrace,
368+
) {
369+
_stopOperation = null;
370+
Error.throwWithStackTrace(error, stackTrace);
371+
});
372+
_stopOperation = nextOperation;
373+
return nextOperation;
362374
}
363375

364376
Future<CoreProcessStopResult> _stop() async {

lib/core/desktop/launcher.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,18 @@ final class DirectCoreLease implements CoreProcessLease {
6969

7070
@override
7171
Future<CoreProcessStopResult> stop(Duration timeout) {
72-
return _stopOperation ??= _stop(timeout);
72+
final stopOperation = _stopOperation;
73+
if (stopOperation != null) {
74+
return stopOperation;
75+
}
76+
final nextOperation = _stop(timeout).then((result) {
77+
if (!result.exitConfirmed) {
78+
_stopOperation = null;
79+
}
80+
return result;
81+
});
82+
_stopOperation = nextOperation;
83+
return nextOperation;
7384
}
7485

7586
Future<CoreProcessStopResult> _stop(Duration timeout) async {

test/core/desktop/helper_client_test.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,50 @@ void main() {
151151
expect(result.exitConfirmed, isTrue);
152152
});
153153

154+
test('Helper lease retries stop after a transport failure', () async {
155+
var stopRequests = 0;
156+
final client = _client(
157+
_ResponseAdapter((options) {
158+
if (options.path.endsWith('/start')) {
159+
return _jsonResponse({'sessionId': _sessionId, 'pid': 6456});
160+
}
161+
stopRequests++;
162+
if (stopRequests == 1) {
163+
throw DioException(
164+
requestOptions: options,
165+
type: DioExceptionType.connectionError,
166+
);
167+
}
168+
return _jsonResponse({
169+
'sessionId': _sessionId,
170+
'stopped': false,
171+
'reason': 'notRunning',
172+
});
173+
}),
174+
);
175+
final launcher = WindowsHelperLauncher(client);
176+
final lease = await launcher.start(
177+
sessionId: _sessionId,
178+
address: 'test-address',
179+
);
180+
181+
await expectLater(
182+
lease.stop(const Duration(seconds: 1)),
183+
throwsA(
184+
isA<WindowsHelperException>().having(
185+
(error) => error.code,
186+
'code',
187+
'transportError',
188+
),
189+
),
190+
);
191+
final result = await lease.stop(const Duration(seconds: 1));
192+
193+
expect(stopRequests, 2);
194+
expect(result.stopped, isFalse);
195+
expect(result.exitConfirmed, isTrue);
196+
});
197+
154198
test(
155199
'Helper launcher compensates an uncertain start with exact stop',
156200
() async {

test/core/desktop/launcher_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ void main() {
5656
expect(result.stopped, isTrue);
5757
expect(result.exitConfirmed, isFalse);
5858
});
59+
60+
test('direct lease rechecks exit after an unconfirmed timeout', () async {
61+
final exitCode = Completer<int>();
62+
final process = _FakeProcess(pid: 42, exitCode: exitCode.future);
63+
final launcher = DirectCoreLauncher(
64+
startProcess: (_, _) async => process,
65+
corePath: 'FlClashCore',
66+
);
67+
final lease = await launcher.start(
68+
sessionId: '0123456789abcdef0123456789abcdef',
69+
address: 'test-address',
70+
);
71+
72+
final firstResult = await lease.stop(Duration.zero);
73+
exitCode.complete(0);
74+
final secondResult = await lease.stop(const Duration(seconds: 1));
75+
76+
expect(firstResult.exitConfirmed, isFalse);
77+
expect(secondResult.exitConfirmed, isTrue);
78+
});
5979
}
6080

6181
class _FakeProcess implements Process {

0 commit comments

Comments
 (0)