From f8afa2d95b87146950623f2821699f2bf5439de8 Mon Sep 17 00:00:00 2001 From: Alexander Hartl Date: Thu, 6 Jul 2023 14:20:43 +0200 Subject: [PATCH] Work around for https://github.com/jupyter/notebook/issues/6721 --- tornado/iostream.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tornado/iostream.py b/tornado/iostream.py index a408be59cd..15d1ddd5cc 100644 --- a/tornado/iostream.py +++ b/tornado/iostream.py @@ -33,6 +33,7 @@ import ssl import sys import re +import threading from tornado.concurrent import Future, future_set_result_unless_cancelled from tornado import ioloop @@ -278,6 +279,7 @@ def __init__( self._connecting = False self._state = None # type: Optional[int] self._closed = False + self._write_lock = threading.Lock() def fileno(self) -> Union[int, ioloop._Selectable]: """Returns the file descriptor for this stream.""" @@ -959,10 +961,11 @@ def _handle_write(self) -> None: # with more than 128KB at a time. size = 128 * 1024 - num_bytes = self.write_to_fd(self._write_buffer.peek(size)) - if num_bytes == 0: - break - self._write_buffer.advance(num_bytes) + with self._write_lock: + num_bytes = self.write_to_fd(self._write_buffer.peek(size)) + if num_bytes == 0: + break + self._write_buffer.advance(num_bytes) self._total_write_done_index += num_bytes except BlockingIOError: break