Skip to content

fix(ffi): clear BoringSSL errors left by other code before each operation - #476

Draft
mosuem wants to merge 1 commit into
use-package-boringfrom
clear-boringssl-errors
Draft

mosuem wants to merge 1 commit into
use-package-boringfrom
clear-boringssl-errors

Conversation

@mosuem

@mosuem mosuem commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Note

This PR was generated by an AI coding agent (Jetski) on behalf of @mosuem.

Replaces #474 (recreated on google/webcrypto.dart and stacked on #475 via gh stack).

With package:boring, every package using BoringSSL on a thread shares one error queue, whereas webcrypto's own copy of BoringSSL had a separate one. An error that another package leaves behind is then reported as the message of webcrypto's next failure, because extractBoringSslError() reports the least recent (most specific) error on the queue.

For example, after another package left an EC UNKNOWN_GROUP error on the queue:

RsaOaepPrivateKey.importPkcs8Key([1, 2, 3], Hash.sha256)
FormatException: error:0f00007b:elliptic curve routines:OPENSSL_internal:UNKNOWN_GROUP

instead of ...:public key routines:OPENSSL_internal:DECODE_ERROR.

Change

  • Add _arena / _arenaStream, which clear the queue and then run BoringArena.using / BoringArena.stream, and use them for all 31 BoringSSL call sites in lib/src/impl_ffi/. This follows the advice in package:boring's README ("Call ERR_clear_error() before a call whose errors you report").
  • No other isolate can run on the thread until the operation awaits, and webcrypto already consumes errors right after each failing call. So this covers every synchronous call.
  • Results don't change, because success and failure come from return values. Only error messages could be wrong.
  • test/ffi/stale_boringssl_errors_test.dart leaves a stale error, then checks that RSA and EC import failures report their own errors and leave the queue empty. It fails without the fix.

All 1585 VM tests pass locally.

…tion

With package:boring, every package using BoringSSL on a thread shares its
error queue, while webcrypto's own copy of BoringSSL had a separate one. An
error that another package left behind was reported as the message of
webcrypto's next failure, because extractBoringSslError() reports the least
recent (most specific) error. For example, a leftover EC UNKNOWN_GROUP error
replaced the DECODE_ERROR of a malformed RSA PKCS#8 key.

Run all BoringSSL calls in _arena / _arenaStream, which clear the queue
before the operation, like package:boring's README recommends. No other
isolate runs on the thread until the operation awaits, and errors are already
consumed right after each failing call, so this covers every synchronous
call. Results don't change: only error messages could be wrong.
@mosuem
mosuem force-pushed the clear-boringssl-errors branch from 5218aa2 to 36756a5 Compare November 19, 2026 12:48

This branch has not been deployed

No deployments
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