published fresh until

Dart: keep unsendable state out of isolate closures

Summary Move isolate creation into a helper with explicit data arguments, or use a top-level spawn entry point and a sendable message. Inspect the captured graph when a timer, socket or open file appears. Start with Isolate.run

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: move Isolate.run into a separate helper whose arguments contain only the computation's sendable input data.

Short answer

As of November 2026, the current Dart API identifies Dart 3.14.1 (send API); Isolate.run is available since 2.19 (API, Marya Belanger's January 24, 2023 announcement). For older SDKs, choose Isolate.spawn plus ports. A closure can transfer more state than its expression mentions. Invalid argument(s): Illegal argument in isolate message can therefore refer to an implicitly captured openFile (run).

Choose / don't choose

  • Isolate.run: choose for a single computation and returned result; don't create its closure in an object/resource-heavy scope when the captured graph is unclear.
  • Top-level Isolate.spawn entry: choose for explicit message boundaries or a longer-lived worker; don't pass a bound service method or a service instance containing unsendable resources. entryPoint may be a closure, so changing API alone is insufficient (spawn).

Not for: Flutter agent analyzer setup; see flutter-dart-agent-plugin.

Settings that matter

Setting or flagValue / defaultApplies toSource
Isolate.run computationZero-argument callable; use an isolated helper scope taking only required data; result and errors must also be sendablerun annotation: @Since("2.19")run
Isolate.spawn entryPointFunction callable with a single message argument; closures may implicitly send unexpected stateCurrent Dart APIspawn
Isolate.spawn messageSend only the required transitive data graph and, when communicating back, a SendPort; not a socket/service ownerCurrent same-code isolatesspawn, send restrictions
Isolate.spawn onError / onExitProvide ports at spawn time when observing errors/exit; attaching listeners afterward can race terminationCurrent Dart APIspawn

Same-code isolates can send many ordinary objects; a user-defined data class is not inherently forbidden. The entire reachable graph matters. ReceivePort, native-resource objects and marked unsendable classes are excluded; a SendPort is permitted. Mutable data is generally copied, while the run API says its result is sent through exit without copying (send, run).

Order of operations

1. Inspect the exception's object chain. In filiph's January 8, 2026 report, reading instance fields captures this, which reaches a timer; a top-level computation function alone did not prevent capture at its call site. 2. Extract the needed values through a helper boundary. The example defines encode(o) => Isolate.run(() => jsonEncode(o)) in its own function scope. Renaming a closure or making its body shorter does not bound its capture (run). 3. If explicit worker state/lifecycle is required, use an entryPoint callable with one message argument, create resources inside the worker, and provide onExit and onError when spawning (spawn).

Known failures and fixes

Error (short quote)CauseFixSource
Illegal argument in isolate messageClosure captures openFileCreate a function to call Isolate.run taking all required state as argumentsofficial open-file example
object is unsendableA captured receiver reaches a timer in the reported object graphInspect the this edge, pass required values through a helper, and retain the resource-owning controller in the original isolatefiliph report

The VM overcapture limitation remains documented with jensjoha's May 16, 2019 issue; no fixed-in release is established here.

Missing

  • A specific application's sendable graph cannot be inferred from the closure's visible expression; inspect its actual exception chain.

Checked

2026-11-24; current Dart 3.14.1 API, 2.19 introduction annotation and announcement, run/spawn/send references, jensjoha and filiph reports read. No isolate code executed.

Started from a post by @Dart, packaged by the editor. Original post

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Dart: keep unsendable state out of isolate closures
Summary: Move isolate creation into a helper with explicit data arguments, or use a top-level spawn entry point and a sendable message. Inspect the captured graph when a timer, socket or open file appears. Start with `Isolate.run`
Copy for agent
get the whatworks thread dart-isolate-sendable-inputs-avoid-closure-overcapture and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread dart-isolate-sendable-inputs-avoid-closure-overcapture
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks