Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: file descriptor conflicts in terminal ws (#8896)

Myles Scolnick56a061d46cfe
diff --git a/.github/renovate.json5 b/.github/renovate.json5
index 88a3a57436..9c682da3b3 100644
--- a/.github/renovate.json5
+++ b/.github/renovate.json5
@@ -1,7 +1,7 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": ["config:recommended", "schedule:monthly"],
- "minimumReleaseAge": "3 days",
+ "minimumReleaseAge": "7 days",
"labels": ["dependencies"],
"pin": {
"enabled": false
diff --git a/marimo/_server/api/endpoints/terminal.py b/marimo/_server/api/endpoints/terminal.py
index d0788c4156..f69d545ea8 100644
--- a/marimo/_server/api/endpoints/terminal.py
+++ b/marimo/_server/api/endpoints/terminal.py
@@ -4,7 +4,7 @@ from __future__ import annotations
import asyncio
import json
import os
-import select
+import selectors
import signal
import struct
import sys
@@ -171,50 +171,65 @@ def _manage_command_buffer(
return buffer
+def _wait_for_read(master: int, timeout: float) -> bool:
+ """Wait for data to be available on the file descriptor.
+
+ Uses selectors instead of select.select() to avoid the FD_SETSIZE
+ limitation (fd must be < 1024) that causes failures when many file
+ descriptors are open.
+ """
+ sel = selectors.DefaultSelector()
+ try:
+ sel.register(master, selectors.EVENT_READ)
+ events = sel.select(timeout=timeout)
+ return len(events) > 0
+ finally:
+ sel.close()
+
+
async def _read_from_pty(master: int, websocket: WebSocket) -> None:
- """Read data from PTY and send to websocket with proper buffering."""
+ """Read data from PTY and send to websocket with proper buffering.
+
+ Uses raw os.read() instead of os.fdopen() to avoid double-close issues
+ when both reader and writer share the same fd.
+ """
loop = asyncio.get_running_loop()
buffer = b""
try:
- with os.fdopen(master, "rb", buffering=0) as master_file:
- while True:
- try:
- # Check for available data with a timeout
- r, _, _ = await loop.run_in_executor(
- None,
- select.select,
- [master_file],
- [],
- [],
- READ_TIMEOUT,
- )
-
- if r:
- # Read available data
- try:
- chunk = os.read(master, MAX_CHUNK_SIZE)
- if not chunk:
- break
- buffer += chunk
- except OSError as e:
- if (
- e.errno == 5
- ): # Input/output error (process died)
- break
- raise
-
- # Send buffered data if we have any
- if buffer:
- text, buffer = _decode_pty_data(buffer)
- if text:
- await websocket.send_text(text)
- else:
- # Small delay to prevent busy-waiting when no data
- await asyncio.sleep(IDLE_SLEEP)
-
- except (asyncio.CancelledError, WebSocketDisconnect):
- break
+ while True:
+ try:
+ # Check for available data with a timeout
+ has_data = await loop.run_in_executor(
+ None,
+ _wait_for_read,
+ master,
+ READ_TIMEOUT,
+ )
+
+ if has_data:
+ # Read available data
+ try:
+ chunk = os.read(master, MAX_CHUNK_SIZE)
+ if not chunk:
+ break
+ buffer += chunk
+ except OSError as e:
+ if e.errno == 5: # Input/output error (process died)
+ break
+ raise
+
+ # Send buffered data if we have any
+ if buffer:
+ text, buffer = _decode_pty_data(buffer)
+ if text:
+ await websocket.send_text(text)
+ else:
+ # Small delay to prevent busy-waiting when no data
+ await asyncio.sleep(IDLE_SLEEP)
+
+ except (asyncio.CancelledError, WebSocketDisconnect):
+ break
except OSError as e:
if e.errno == 9: # Bad file descriptor
LOGGER.debug("File descriptor closed, stopping read loop")
@@ -263,50 +278,48 @@ async def _maybe_handle_resize(
async def _write_to_pty(
master: int, websocket: WebSocket, child_pid: int
) -> None:
- """Write data from websocket to PTY with command monitoring and resize handling."""
+ """Write data from websocket to PTY with command monitoring and resize handling.
+
+ Uses raw os.write() instead of os.fdopen() to avoid double-close issues
+ when both reader and writer share the same fd.
+ """
try:
command_buffer = ""
- with os.fdopen(master, "wb", buffering=0) as master_file:
- while True:
- try:
- data = await websocket.receive_text()
- LOGGER.debug("Received: %s", repr(data))
-
- # Check if this is a resize message
- if await _maybe_handle_resize(
- master=master, child_pid=child_pid, message=data
- ):
- continue
-
- # Handle special key combinations and commands
- command_buffer = _manage_command_buffer(
- command_buffer, data
- )
-
- # Check for exit command
- if _should_close_on_command(command_buffer, data):
- LOGGER.debug(
- "Exit command received, closing connection"
- )
- return
-
- # Reset buffer on line endings
- if data in ["\r", "\n"]:
- command_buffer = ""
-
- # Write data to PTY
- try:
- encoded_data = data.encode("utf-8")
- master_file.write(encoded_data)
- master_file.flush()
- except OSError as e:
- if e.errno == 5: # Input/output error (process died)
- LOGGER.debug("Process died, stopping write loop")
- break
- raise
+ while True:
+ try:
+ data = await websocket.receive_text()
+ LOGGER.debug("Received: %s", repr(data))
+
+ # Check if this is a resize message
+ if await _maybe_handle_resize(
+ master=master, child_pid=child_pid, message=data
+ ):
+ continue
+
+ # Handle special key combinations and commands
+ command_buffer = _manage_command_buffer(command_buffer, data)
- except (asyncio.CancelledError, WebSocketDisconnect):
- break
+ # Check for exit command
+ if _should_close_on_command(command_buffer, data):
+ LOGGER.debug("Exit command received, closing connection")
+ return
+
+ # Reset buffer on line endings
+ if data in ["\r", "\n"]:
+ command_buffer = ""
+
+ # Write data to PTY
+ try:
+ encoded_data = data.encode("utf-8")
+ os.write(master, encoded_data)
+ except OSError as e:
+ if e.errno == 5: # Input/output error (process died)
+ LOGGER.debug("Process died, stopping write loop")
+ break
+ raise
+
+ except (asyncio.CancelledError, WebSocketDisconnect):
+ break
except OSError as e:
if e.errno == 9: # Bad file descriptor
LOGGER.debug("File descriptor closed, stopping write loop")