[objective_c] Receive port cleanup missing when block trampoline resolution fails

Open Beginner friendly
#3,841 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
dart
Domain
mobile-dev

Research direction

Read pkgs/objective_c/lib/src/internal.dart, especially newBlockPort and newBlockingBlockPort. Trace what resources each helper creates before invoking maker, then find the relevant tests for trampoline resolution failures. Ensure those resources are released while the original ArgumentError is preserved, and confirm the receive port no longer keeps the process alive.

Written by the indexing model from the issue text.

Description

lang-objective_c needs-triage package:objective_c

While investigating #2953, I noticed that newBlockPort and newBlockingBlockPort do not clean up allocated resources when native trampoline resolution fails.

Both helpers create a RawReceivePort before invoking maker. If native symbol resolution throws an ArgumentError, attachPortBlockFinalizer is never reached, leaving the receive port open. In the blocking variant, the NativeCallable may also remain unclosed.

Relevant source:

Minimal reproduction
import 'dart:ffi';
import 'dart:isolate';

@Native<Int32 Function()>(
  symbol: 'intentionally_missing_symbol',
  isLeaf: true,
)
external int missingFunction();

void createBlock(int Function() maker, bool cleanup) {
  final port = RawReceivePort()
    ..keepIsolateAlive = true;

  port.handler = (message) {};

  try {
    maker();
  } catch (_) {
    if (cleanup) {
      port.close();
    }
    rethrow;
  }

  port.close();
}

void main(List<String> args) {
  try {
    createBlock(missingFunction, args.contains('--cleanup'));
  } catch (e) {
    print(e.runtimeType);
    print(e);
  }

  print('End of main');
}
Steps to reproduce
timeout 5s dart repro.dart
echo "Exit code: $?"

timeout 5s dart repro.dart --cleanup
echo "Exit code: $?"
Observed behavior

Without cleanup:

ArgumentError
...
End of main
Exit code: 124

With cleanup:

ArgumentError
...
End of main
Exit code: 0

Without cleanup, the process hangs despite reaching the end of main. Explicitly closing the receive port allows the process to terminate normally.

Expected behavior

Resources allocated before invoking the native trampoline should be released if trampoline resolution fails, while preserving the original error.

Environment
  • Dart SDK: 3.13.2 (stable)
  • Platform: Linux x64 (WSL)
  • Upstream commit: 2c63bb68a

The minimal reproduction demonstrates the failure pattern independently of package:objective_c. I haven't verified the full Objective-C integration scenario on macOS or independently reproduced the potential NativeCallable leak.

Dominant language
Dart
Stars
280
Forks
166
Avg merge
3d 4h
Merged PRs (30d)
33

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dart-lang/native

All issues in dart-lang/native

Similar issues

More Dart issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.