fix: properly clean up timer and stream controller in OAuth session handling

- Store timeout timer reference and cancel it in finally block to prevent
stale timer from firing after operation completes
- Check if stream controller is closed before adding cancellation event
to prevent "Cannot add event after closing" error

Changed files
+7 -2
packages
atproto_oauth_flutter
lib
+2 -1
packages/atproto_oauth_flutter/lib/src/session/session_getter.dart
···
// Make sure, even if there is no signal in the options, that the
// request will be cancelled after at most 30 seconds.
final timeoutToken = CancellationToken();
-
Timer(Duration(seconds: 30), () => timeoutToken.cancel());
+
final timeoutTimer = Timer(Duration(seconds: 30), () => timeoutToken.cancel());
final combinedSignal =
options?.signal != null
···
),
);
} finally {
+
timeoutTimer.cancel(); // Cancel timer before disposing token
combinedSignal.dispose();
timeoutToken.dispose();
}
+5 -1
packages/atproto_oauth_flutter/lib/src/util.dart
···
_isCancelled = true;
_reason = reason ?? Exception('Operation was cancelled');
-
_controller.add(null);
+
+
// Only add to stream if not already closed
+
if (!_controller.isClosed) {
+
_controller.add(null);
+
}
}
/// Throw an exception if the operation has been cancelled.