Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue where large stdin writes can cause Tornado to hang #189

Merged
merged 6 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ jobs:
- uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
- uses: jupyterlab/maintainer-tools/.github/actions/check-links@v1

jupyter_server_terminals:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v2
- uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
- uses: jupyterlab/maintainer-tools/.github/actions/downstream-test@v1
with:
package_name: jupyter_server_terminals

# Run "pre-commit run --all-files --hook-stage=manual"
pre-commit:
name: Run pre-commit hook
Expand Down
43 changes: 43 additions & 0 deletions demos/custom_exec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Using a custom thread pool for subprocess writes.
"""
from concurrent import futures

import tornado.web

# This demo requires tornado_xstatic and XStatic-term.js
import tornado_xstatic
from common_demo_stuff import STATIC_DIR, TEMPLATE_DIR, run_and_show_browser

from terminado import SingleTermManager, TermSocket


class TerminalPageHandler(tornado.web.RequestHandler):
def get(self):
return self.render(
"termpage.html",
static=self.static_url,
xstatic=self.application.settings["xstatic_url"],
ws_url_path="/websocket",
)


def main(argv):
with futures.ThreadPoolExecutor(max_workers=2) as custom_exec:
term_manager = SingleTermManager(shell_command=["bash"], blocking_io_executor=custom_exec)
handlers = [
(r"/websocket", TermSocket, {"term_manager": term_manager}),
(r"/", TerminalPageHandler),
(r"/xstatic/(.*)", tornado_xstatic.XStaticFileHandler, {"allowed_modules": ["termjs"]}),
]
app = tornado.web.Application(
handlers,
static_path=STATIC_DIR,
template_path=TEMPLATE_DIR,
xstatic_url=tornado_xstatic.url_maker("/xstatic/"),
)
app.listen(8765, "localhost")
run_and_show_browser("http://localhost:8765/", term_manager)


if __name__ == "__main__":
main([])
18 changes: 17 additions & 1 deletion terminado/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import signal
import warnings
from collections import deque
from concurrent import futures

try:
from ptyprocess import PtyProcessUnicode
Expand Down Expand Up @@ -153,7 +154,13 @@ class TermManagerBase:
"""Base class for a terminal manager."""

def __init__(
self, shell_command, server_url="", term_settings=None, extra_env=None, ioloop=None
self,
shell_command,
server_url="",
term_settings=None,
extra_env=None,
ioloop=None,
blocking_io_executor=None,
):
self.shell_command = shell_command
self.server_url = server_url
Expand All @@ -163,6 +170,13 @@ def __init__(

self.ptys_by_fd = {}

if blocking_io_executor is None:
self._blocking_io_executor_is_external = False
self.blocking_io_executor = futures.ThreadPoolExecutor(max_workers=1)
else:
self._blocking_io_executor_is_external = True
self.blocking_io_executor = blocking_io_executor

if ioloop is not None:
warnings.warn(
f"Setting {self.__class__.__name__}.ioloop is deprecated and ignored",
Expand Down Expand Up @@ -259,6 +273,8 @@ def client_disconnected(self, websocket):

async def shutdown(self):
await self.kill_all()
if not self._blocking_io_executor_is_external:
self.blocking_io_executor.shutdown(wait=False, cancel_futures=True)

async def kill_all(self):
futures = []
Expand Down
Loading