Skip to content

Commit a770650

Browse files
committed
Fix RPC write_message to use unbuffered binary I/O on Windows
The write_message function was using text mode (sys.stdout.write) which causes Python to perform automatic line-ending translation on Windows. This corrupts the JSON-RPC protocol headers which expect exact byte sequences like 'Content-Length: N\r\n\r\n'. Use unbuffered binary I/O (os.write) instead, mirroring the pattern already used by read_message and read_message_with_timeout which use os.read(). This ensures: - No platform-specific line-ending translation - Symmetric read/write I/O patterns - Atomic writes for small RPC messages (within PIPE_BUF) - Single system call (same as before) Fixes the 10-second timeout failures on Windows runners where the Python RPC server couldn't send responses to Java due to protocol header corruption.
1 parent e804713 commit a770650

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

  • rewrite-python/rewrite/src/rewrite/rpc

rewrite-python/rewrite/src/rewrite/rpc/server.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,14 +1380,15 @@ def read_bytes(n: int) -> Optional[bytes]:
13801380

13811381

13821382
def write_message(response: dict):
1383-
"""Write a JSON-RPC message to stdout."""
1384-
content = json.dumps(response)
1385-
content_bytes = content.encode('utf-8')
1386-
1387-
sys.stdout.write(f"Content-Length: {len(content_bytes)}\r\n")
1388-
sys.stdout.write("\r\n")
1389-
sys.stdout.write(content)
1390-
sys.stdout.flush()
1383+
"""Write a JSON-RPC message to stdout.
1384+
1385+
Uses unbuffered binary I/O to avoid line-ending translation on Windows
1386+
that would corrupt the JSON-RPC protocol headers. Mirrors the pattern
1387+
used by read_message() which uses os.read() on the read side.
1388+
"""
1389+
content_bytes = json.dumps(response).encode('utf-8')
1390+
header = f"Content-Length: {len(content_bytes)}\r\n\r\n".encode('utf-8')
1391+
os.write(sys.stdout.fileno(), header + content_bytes)
13911392

13921393

13931394
def main():

0 commit comments

Comments
 (0)