@@ -276,6 +276,10 @@ class FileExplorerController extends Notifier<FileExplorerState> {
276276 /// Invalidates late file-open completions when a newer open starts.
277277 int _openEpoch = 0 ;
278278
279+ /// Invalidates filesystem-root discovery when its initiating attachment is
280+ /// replaced, the controller is disposed, or a newer initialization starts.
281+ int _initEpoch = 0 ;
282+
279283 /// Serializes explicit Open-as-Blocks source downloads. The native row stays
280284 /// responsive, but a rapid second activation cannot start a duplicate GET
281285 /// or a competing preview flow.
@@ -295,7 +299,10 @@ class FileExplorerController extends Notifier<FileExplorerState> {
295299 final ValueListenable <ConnState > connState = ref.watch (connStateProvider);
296300 _lastConnState = connState.value;
297301 connState.addListener (_onConnState);
298- ref.onDispose (() => connState.removeListener (_onConnState));
302+ ref.onDispose (() {
303+ _initEpoch += 1 ;
304+ connState.removeListener (_onConnState);
305+ });
299306
300307 if (_isAttached (connState.value)) {
301308 // Already connected at first build (the gated shell's normal path):
@@ -322,6 +329,9 @@ class FileExplorerController extends Notifier<FileExplorerState> {
322329 // A NEW board session became ready: auto-load its listing (FR-FILES-1).
323330 _init ();
324331 } else if (now == ConnState .disconnected) {
332+ // DEVICE_INFO and the initial listing belong to the attachment that
333+ // started them. Prevent either completion from publishing after detach.
334+ _initEpoch += 1 ;
325335 // The board went away: drop the stale listing (the view shows the
326336 // disconnected guidance); the next connection re-roots via _init.
327337 state = state.copyWith (
@@ -338,25 +348,57 @@ class FileExplorerController extends Notifier<FileExplorerState> {
338348
339349 /// Reads `fsRoot` from the board and lists the root directory.
340350 Future <void > _init () async {
351+ final int epoch = ++ _initEpoch;
352+ final Connection connection = _conn;
353+ final Object sessionStamp = connectionSessionStampOf (connection);
354+ String ? reportedRoot;
341355 state = state.copyWith (
342356 loading: true ,
343357 hasReportedFsRoot: false ,
344358 clearError: true ,
345359 );
346360 try {
347- final DeviceInfo info = await _conn.deviceInfo ();
348- _fsRoot = info.fsRoot;
361+ final DeviceInfo info = await connection.deviceInfo ();
362+ if (! _isInitContextCurrent (
363+ epoch: epoch,
364+ connection: connection,
365+ stamp: sessionStamp,
366+ )) {
367+ return ;
368+ }
369+
370+ reportedRoot = info.fsRoot;
371+ _fsRoot = reportedRoot;
349372 state = state.copyWith (
350373 fsRoot: _fsRoot,
351374 hasReportedFsRoot: true ,
352375 cwd: _fsRoot,
353376 );
354- await refresh ();
377+
378+ final List <RemoteEntry > entries = await connection.listDir (reportedRoot);
379+ if (! _isInitContextCurrent (
380+ epoch: epoch,
381+ connection: connection,
382+ stamp: sessionStamp,
383+ ) ||
384+ state.cwd != reportedRoot) {
385+ return ;
386+ }
387+ entries.sort (_entryOrder);
388+ state = state.copyWith (entries: entries, loading: false );
355389 } on PbleException catch (e) {
390+ if (! _isInitContextCurrent (
391+ epoch: epoch,
392+ connection: connection,
393+ stamp: sessionStamp,
394+ ) ||
395+ (reportedRoot != null && state.cwd != reportedRoot)) {
396+ return ;
397+ }
356398 state = state.copyWith (
357399 loading: false ,
358400 error: _kindOf (e),
359- errorPath: _fsRoot,
401+ errorPath: reportedRoot ?? _fsRoot,
360402 );
361403 }
362404 }
@@ -744,6 +786,15 @@ class FileExplorerController extends Notifier<FileExplorerState> {
744786 identical (connectionSessionStampOf (current), stamp);
745787 }
746788
789+ bool _isInitContextCurrent ({
790+ required int epoch,
791+ required Connection connection,
792+ required Object stamp,
793+ }) =>
794+ epoch == _initEpoch &&
795+ _isCapturedSessionCurrent (connection: connection, stamp: stamp) &&
796+ _isAttached (connection.state.value);
797+
747798 /// Wall-clock for the transfer in flight; restarted per transfer.
748799 Stopwatch ? _txClock;
749800 int _lastSent = 0 ;
0 commit comments