Skip to content

APISecurity: onNewAPIToken / onLogout return FutureOr<void>, awaited (v1.37.0) - #214

Merged
gmpassos merged 2 commits into
masterfrom
feature/security-hooks-futureor
Nov 29, 2026
Merged

gmpassos merged 2 commits into
masterfrom
feature/security-hooks-futureor

Conversation

@gmpassos

@gmpassos gmpassos commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

APISecurity: onNewAPIToken / onLogout return FutureOr<void> and are awaited (v1.37.0)

Why. Both hooks were void, called without being awaited. A subclass storing something in them, as an app persisting the refresh token, got detached work:

  • it outlived the request that issued the token;
  • its error was an unhandled async error.

In AllDely's CI, the last test of a file issued a token, then tearDownAll closed the database while the hook's store still ran. The result was Bad state: Closed DBAdapter and a test that "failed after test completion", on some runs only.

How the hooks are called. They go through async_extension's new tryCallWithValue (1.2.25, now on pub.dev), which keeps the FutureOr behavior:

// in _resolveAuthenticationImpl:
.resolveMapped((apiTokenInfo) {
  if (apiTokenInfo != null && changedAPIToken) {
    return (() => onNewAPIToken(apiTokenInfo.apiToken, false, request: request))
        .tryCallWithValue(authentication, onError: _logHookError('onNewAPIToken'));
  }
  return authentication;
})
// refreshAPIToken: ... .tryCallWithValue(apiToken, onError: ...)
// logout: onLogout.tryCallWithValue<APIAuthentication?>(credential, null, onError: ...)
  • A synchronous hook gives the value directly. No Future or microtask is added, so a synchronous chain stays synchronous.
  • A hook returning a Future is chained: the authentication, refresh or logout completes after it.
  • A hook that fails, by a synchronous throw or an async error, is logged as severe and never fails the operation, which already happened.

Compatibility.

  • Source compatible: void and FutureOr<void> are mutual subtypes, so existing void overrides still compile.
  • An override declared void ... async returns a Future at runtime and is now awaited. That is the behavior change, hence the minor version.

Tests (bones_api_cov_security_test.dart)

A _HookSecurity with sync / async / throws / asyncError hooks covers:

  • an async hook is done before authenticate, refreshAPIToken and logout complete;
  • a sync hook keeps authenticate and refreshAPIToken synchronous;
  • a failing hook is logged, and the operation still succeeds.

Without the change, 3 of these fail; the asyncError one fails "after it had already completed". dart analyze is clean and dart pub publish --dry-run has 0 warnings.

🤖 Generated with Claude Code

… errors logged (v1.37.0)

A hook doing async work (storing the refresh token) ran detached: it
outlived the request, and its error was an unhandled async error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.97%. Comparing base (6a30d36) to head (8233488).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #214      +/-   ##
==========================================
+ Coverage   95.95%   95.97%   +0.02%     
==========================================
  Files          75       75              
  Lines       26035    26042       +7     
==========================================
+ Hits        24982    24994      +12     
+ Misses       1053     1048       -5     
Flag Coverage Δ
unittests 95.97% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…2.25)

Replaces the private _callHook: same behavior (sync stays sync, errors
logged, never propagated).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gmpassos
gmpassos merged commit 2c0a5e2 into master Nov 29, 2026
11 checks passed
@gmpassos
gmpassos deleted the feature/security-hooks-futureor branch November 29, 2026 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant