Skip to content

Commit bf02c84

Browse files
authored
[libc] Use file lock to join newline on RPC puts call (#73373)
Summary: The puts call appends a newline. With multiple threads, this can be done out of order such that another thread puts something before we finish appending the newline. Add a flockfile and funlockfile to ensure that the whole string is printed before another string can appear.
1 parent 839abdb commit bf02c84

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

libc/utils/gpu/server/rpc_server.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ struct Server {
7878

7979
port->recv_n(strs, sizes, [&](uint64_t size) { return new char[size]; });
8080
port->send([&](rpc::Buffer *buffer, uint32_t id) {
81-
buffer->data[0] = fwrite(strs[id], 1, sizes[id], files[id]);
81+
flockfile(files[id]);
82+
buffer->data[0] = fwrite_unlocked(strs[id], 1, sizes[id], files[id]);
8283
if (port->get_opcode() == RPC_WRITE_TO_STDOUT_NEWLINE &&
8384
buffer->data[0] == sizes[id])
84-
buffer->data[0] += fwrite("\n", 1, 1, files[id]);
85+
buffer->data[0] += fwrite_unlocked("\n", 1, 1, files[id]);
86+
funlockfile(files[id]);
8587
delete[] reinterpret_cast<uint8_t *>(strs[id]);
8688
});
8789
break;

0 commit comments

Comments
 (0)