Skip to content

[ffigen] Support blocking callbacks #1647

@liamappelbe

Description

@liamappelbe

Turns out there are a few Apple APIs that require that the completion handler is invoked during the callback. So we should add a variant of listener blocks and protocol methods that uses a mutex to block until the listener callback is complete.

  • We'll need a native trampoline similar to the ref counting trampoline that listeners get, but instead of incrementing the ref count it will lock the mutex.
  • If the callback is invoked on the same thread as the isolate that owns the listener, it will deadlock. So we should detect this case and invoke the callback synchronously (ie NativeCallable.isolateLocal style).
  • If the isolate has shut down before the callback is invoked, the caller thread will wait forever. So add a timeout that throws an assertion failure if the callback doesn't run in N seconds. This error won't have a ton of context, but it's better than silently deadlocking (or more realistically, having some background thread that's sitting there doing nothing).

With a little more infrastructure, it would also be possible to support callbacks that return results, but these are even rarer in Apple APIs, so we'll wait until we have a use case for this.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions