Skip to content

Commit 6b6f7f9

Browse files
NelsonVidesfrazze-jobb
authored andcommitted
Fix race condition in standard_error process registration
Fix a race condition where the standard_error process could call prim_tty:init/1 before being registered, causing a crash when prim_tty's writer and reader processes attempt to derive their own registered names from the parent process. The issue was introduced in commit ead7424 (PR #9116), which changed prim_tty's reader and writer processes to dynamically derive their registered names from their parent process name (e.g., 'standard_error' -> 'standard_error_reader', 'standard_error_writer'). This was done to support multiple TTY instances (stdout and stderr) with distinct process names. However, the standard_error:start/0 function had a race condition: 1. spawn(fun server/0) - creates process, starts executing immediately 2. register(?NAME, Id) - parent registers the spawned process 3. server() calls prim_tty:init() - may execute before step 2 When prim_tty:init/1 spawns writer/reader processes, they call set_name(Parent, Postfix) which does: erlang:process_info(Parent, registered_name) If this executes before the parent completes registration, it returns [] instead of {registered_name, standard_error}, causing a pattern match failure: "no match of right hand side value: []" at prim_tty.erl:674. The fix moves the register/2 call into the spawned process itself, ensuring it completes before prim_tty:init/1 is called. This eliminates the race condition entirely. Closes #10174
1 parent b2e2bc4 commit 6b6f7f9

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

lib/kernel/src/standard_error.erl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,11 @@ init([]) ->
6262
end.
6363

6464
start() ->
65-
Id = spawn(fun server/0),
66-
register(?NAME, Id),
67-
Id.
65+
spawn(fun server/0).
6866

6967
server() ->
7068
process_flag(trap_exit, true),
69+
register(?NAME, self()),
7170
ok = prim_tty:load(),
7271
TTY = prim_tty:init(#{ input => disabled,
7372
output => cooked,

0 commit comments

Comments
 (0)