Skip to content

Commit b9105c7

Browse files
committed
[green] finish owned cleanup and serialize same-device acquisition
Signed-off-by: Viwat Vchirawongkwin <viwat.v@chula.ac.th>
1 parent db7ccc4 commit b9105c7

4 files changed

Lines changed: 67 additions & 15 deletions

File tree

‎app/lib/ble/fbp_ble_link.dart‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,23 @@ class FbpBleLink implements BleLink {
4747

4848
@override
4949
Future<void> disconnect() async {
50-
await _device.disconnect();
51-
_linkState.value = BleLinkState.disconnected;
52-
await _connSub.cancel();
50+
Object? firstError;
51+
StackTrace? firstStack;
52+
try {
53+
await _device.disconnect();
54+
_linkState.value = BleLinkState.disconnected;
55+
} catch (error, stack) {
56+
firstError = error;
57+
firstStack = stack;
58+
}
59+
try {
60+
await _connSub.cancel();
61+
} catch (error, stack) {
62+
firstError ??= error;
63+
firstStack ??= stack;
64+
}
65+
if (firstError != null) {
66+
Error.throwWithStackTrace(firstError, firstStack!);
67+
}
5368
}
5469
}

‎app/lib/pble/connection_manager.dart‎

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ class PbleConnectionManager implements ConnectionManager {
163163
int _intent = 0;
164164
Future<void>? _disposeFuture;
165165
final Set<Future<void>> _retirements = <Future<void>>{};
166+
final Map<String, Future<void>> _openings = <String, Future<void>>{};
166167

167168
@override
168169
Connection get connection => _facade;
@@ -241,17 +242,26 @@ class PbleConnectionManager implements ConnectionManager {
241242
// Do not open another board until those exact retirements complete.
242243
await Future.wait(_retirements.toList());
243244
if (!_isCurrent(intent)) return;
244-
final Connection board = await _connectionFactory(id);
245-
if (!_isCurrent(intent)) {
246-
await _retire(board);
247-
return;
245+
if (_openings[id] case final Future<void> previous) {
246+
// Native wrappers with the same ID share one physical link. Finish
247+
// the stale acquisition AND its teardown before opening that ID again.
248+
// Its factory error belongs to its caller; a failed physical close is
249+
// independently retained in _retirements and must still block us.
250+
try {
251+
await previous;
252+
} catch (_) {}
253+
await Future.wait(_retirements.toList());
254+
if (!_isCurrent(intent)) return;
255+
}
256+
final Future<void> opening = Future<void>.microtask(
257+
() => _openAndPublish(id, intent),
258+
);
259+
_openings[id] = opening;
260+
try {
261+
await opening;
262+
} finally {
263+
if (identical(_openings[id], opening)) _openings.remove(id);
248264
}
249-
_liveBoard = board;
250-
_facade.attach(board);
251-
// Derive the phase from the freshly attached board's live state (the
252-
// listener also fires on the sync-up, but re-derive to cover a board that
253-
// attaches already-ready with no state change).
254-
_phase.value = _phaseForConn(_facade.state.value);
255265
} catch (error) {
256266
if (_isCurrent(intent)) {
257267
_lastError = error;
@@ -261,6 +271,18 @@ class PbleConnectionManager implements ConnectionManager {
261271
}
262272
}
263273

274+
Future<void> _openAndPublish(String id, int intent) async {
275+
if (!_isCurrent(intent)) return;
276+
final Connection board = await _connectionFactory(id);
277+
if (!_isCurrent(intent)) {
278+
await _retire(board);
279+
return;
280+
}
281+
_liveBoard = board;
282+
_facade.attach(board);
283+
_phase.value = _phaseForConn(_facade.state.value);
284+
}
285+
264286
@override
265287
Future<void> disconnect() async {
266288
_ensureActive();
@@ -311,7 +333,10 @@ class PbleConnectionManager implements ConnectionManager {
311333
_facade.state.removeListener(_onFacadeState);
312334
_detachAndRetire();
313335
_selected = null;
314-
await attempt(() => Future.wait(_retirements.toList()));
336+
await Future.wait(<Future<void>>[
337+
attempt(_scanner.stopScan),
338+
attempt(() => Future.wait(_retirements.toList())),
339+
]);
315340
await attempt(_scanResults.close);
316341
await attempt(_facade.dispose);
317342
await attempt(_phase.dispose);

‎app/lib/pble/engine.dart‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,14 @@ class PbleEngine {
147147
Future<void> dispose() async {
148148
if (_disposed) return;
149149
_disposed = true;
150-
await _inboundSub.cancel();
150+
Object? cancelError;
151+
StackTrace? cancelStack;
152+
try {
153+
await _inboundSub.cancel();
154+
} catch (error, stack) {
155+
cancelError = error;
156+
cancelStack = stack;
157+
}
151158
for (final _PendingRequest pending in _pending.values) {
152159
if (!pending.completer.isCompleted) {
153160
pending.completer.completeError(
@@ -157,6 +164,9 @@ class PbleEngine {
157164
}
158165
_pending.clear();
159166
await _events.close();
167+
if (cancelError != null) {
168+
Error.throwWithStackTrace(cancelError, cancelStack!);
169+
}
160170
}
161171
}
162172

‎app/lib/pble/pble_connection.dart‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ class PbleConnection implements Connection, ConnectionDirectoryListingSource {
303303
final PbleFrame begin = await _engine.request(
304304
_cmd(PbleOpcode.fileGetBegin, <int>[..._u32(0), ..._pathField(path)]),
305305
);
306+
_ensureActive();
306307
_checkStatus(begin);
307308
final int total = _readU32(begin.payload, 1);
308309

@@ -347,6 +348,7 @@ class PbleConnection implements Connection, ConnectionDirectoryListingSource {
347348
..._pathField(path),
348349
]),
349350
);
351+
_ensureActive();
350352
_checkStatus(begin);
351353
// resume_offset (FR-PBLE-10): a board holding a verified prefix returns it
352354
// so the window starts mid-file — never re-send the verified bytes.

0 commit comments

Comments
 (0)