Skip to content

Commands and Reactors #13

Closed
Closed
@sunfishcode

Description

@sunfishcode

There seem to be two distinct modes of program execution that applications broadly fit into: Commands and Reactors.

  • A Command has a "main" function, and when this function returns the program terminates.
  • A Reactor has a "setup" function, and when this function returns the program remains live, allowing functions registered as callbacks to be called. There is an event loop in the wasm runtime, rather than in the application.

Reactors could run in the main thread of a browser, but they may also have uses in a variety of settings where applications will primarily be responding to external events. Putting the event loop in the runtime gives the runtime the flexiblity to use a single event loop for multiple purposes.

(I briefly mentioned these ideas here, but I want to give them more visibility here.)

Emscripten provides a kind of hybrid approach, where instances can stick around after "main" exits to allow them to be called by callbacks. For WASI, it may be useful to make an explicit static distinction between Commands and Reactors, because:

  • We could limit the APIs available to Reactors. For example, if we want to run Reactors in the main thread of a browser, we could prohibit them from using synchronous I/O APIs.
  • We could limit the APIs available to Commands too, for example not allowing them to register for external event-loop APIs since there is no external event loop in a Command.
  • Allowing runtimes to know that a program is a Reactor up front can enable some useful optimizations, like switching JIT tiers between turns of the event loop, in a very simple way.

Currently, WASI programs combined with clang fit the Command model, with _start being the "main" function. It might make sense to rename it to __wasi_command_main or something. Programs could declare themselves to be Reactors by exporting a function named something like __wasi_reactor_setup or so.

Some interesting questions:

  • Is a static distinction between Command and Reactor too limiting?
  • Are there programs which don't fit into either Command or Reactor models?
  • Are the restrictions imposed by browser main thread execution too limiting for non-Web use cases?

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionA discussion that doesn't yet have a specific conclusion or actionable proposal.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions