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 with adding pending writes #6680

Closed
wants to merge 2 commits into from
Closed
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
21 changes: 15 additions & 6 deletions panel/io/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
)
GC_DEBOUNCE = 5
_WRITE_LOCK = None
_WRITE_FUTURES = weakref.WeakKeyDictionary()
_pending_writes = weakref.WeakKeyDictionary()

def WRITE_LOCK():
global _WRITE_LOCK
Expand Down Expand Up @@ -145,7 +145,7 @@ async def _run_write_futures(doc):
from tornado.websocket import WebSocketClosedError
remaining = {}
async with WRITE_LOCK():
for conn, futures in _WRITE_FUTURES.pop(doc, {}).items():
for conn, futures in _pending_writes.pop(doc, {}).items():
socket = conn._socket
if hasattr(socket, 'write_lock') and socket.write_lock._block._value == 0:
remaining[conn] = futures
Expand All @@ -158,7 +158,13 @@ async def _run_write_futures(doc):
except Exception as e:
logger.warning(f"Failed sending message due to following error: {e}")
if remaining:
_WRITE_FUTURES[doc] = remaining
if doc in _pending_writes:
_pending_writes[doc] = {
conn: writes + _pending_writes[doc].get(conn, [])
for conn, writes in remaining.items()
}
else:
_pending_writes[doc] = remaining
await asyncio.sleep(0.01)
await _run_write_futures(doc)

Expand Down Expand Up @@ -389,10 +395,13 @@ def unlocked() -> Iterator:
else:
futures = dispatch_django(conn, dispatch_events)
send |= bool(futures)
if curdoc in _WRITE_FUTURES:
_WRITE_FUTURES[curdoc][conn].extend(futures)
if curdoc in _pending_writes:
if conn in _pending_writes[curdoc]:
_pending_writes[curdoc][conn].extend(futures)
else:
_pending_writes[curdoc][conn] = futures
else:
_WRITE_FUTURES[curdoc] = {conn: futures}
_pending_writes[curdoc] = {conn: futures}

if send:
if state._unblocked(curdoc):
Expand Down
Loading