8000 Mock.IO captures spawned-process output in the interpreter but not on BEAM · Issue #141 · kexhq/kex · GitHub
[go: up one dir, main page]

Skip to content

Mock.IO captures spawned-process output in the interpreter but not on BEAM #141

Closed

Description

@akoskovacs

Mock.IO (src/stdlib/http.kex:143) captures output written by the calling
process. Whether it also captures output written by a spawned process
depends on the backend, and the two disagree.

BEAM — per-process, spawned output escapes

runtime/src/kex_io.erl keeps the mock state in the process dictionary:

mock_start() ->
    put(kex_mock_io_active, true),
    mock_clear().

mock_active() -> get(kex_mock_io_active) =:= true.

(:66, :89; mock_append/2 at :91 writes to put(kex_mock_io_output, …).)

A spawned process starts with an empty dictionary, so mock_active() is
false there and its writes go to the real stdout — invisible to
Mock.IO.output() and visible in the test runner's own output.

Interpreter — evaluator-wide, spawned output is captured

src/interpreter/stdlib/io.cxx keeps it in evaluator members:

if (m_mockIO) {
    m_mockIOOutput += out;
}

(:39-40, and the same guard at :52, :65, :87, :177.)

m_mockIO / m_mockIOOutput belong to the Evaluator, which every fiber
shares, so output from a spawned fiber lands in the buffer.

Consequence

Mock.IO.start()
spawn do
  IO.printLine("from the child")
end
# ... await
Mock.IO.output()   # interpreter: contains it. BEAM: does not.

A test asserting on output produced by a spawned process passes under one
backend and fails under the other. Which behaviour is right is a design
question, not just a bug to patch:

  • Per-process matches Erlang's default for the process dictionary, and
    keeps concurrent tests from writing into each other's buffers.
  • Inherited is what Erlang actually does for IO, via the group leader —
    a spawned process inherits its parent's, which is precisely why
    io:format/2 from a child still reaches the right place. This is the
    behaviour tests almost always want.

The group leader is the better model, and it is the same mechanism that a
handle-as-a-value design would need (#139). Fixing this by making the mock
inheritable on BEAM is a step toward that rather than away from it.

Whichever is chosen, both backends must agree, and a spec should pin it — the
existing suite does not cover spawn-plus-capture, which is why this went
unnoticed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0