Skip to content

Commit 3e9a1fb

Browse files
committed
Fix prerender SynchronousOnlyOperation bug
1 parent 5341851 commit 3e9a1fb

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ Don't forget to remove deprecated code on each major release!
3232

3333
- Refactoring of internal code to improve maintainability. No changes to publicly documented API.
3434

35+
### Fixed
36+
37+
- Fixed bug where prerendered components could generate a `SynchronousOnlyOperation` exception if they utilize the Django ORM.
38+
3539
## [5.1.1] - 2024-12-02
3640

3741
### Fixed

src/reactpy_django/utils.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import asyncio
56
import contextlib
67
import inspect
78
import logging
@@ -16,7 +17,6 @@
1617
from uuid import UUID, uuid4
1718

1819
import dill
19-
from asgiref.sync import async_to_sync
2020
from channels.db import database_sync_to_async
2121
from django.contrib.staticfiles.finders import find
2222
from django.core.cache import caches
@@ -353,14 +353,18 @@ class SyncLayout(Layout):
353353
"""
354354

355355
def __enter__(self):
356-
async_to_sync(self.__aenter__)()
357-
return self
356+
self.loop = asyncio.new_event_loop()
357+
self.thread = ThreadPoolExecutor(max_workers=1)
358+
return self.thread.submit(self.loop.run_until_complete, self.__aenter__()).result()
358359

359-
def __exit__(self, *_):
360-
async_to_sync(self.__aexit__)(*_)
360+
def __exit__(self, *exec):
361+
result = self.thread.submit(self.loop.run_until_complete, self.__aexit__(*exec)).result()
362+
self.loop.close()
363+
self.thread.shutdown()
364+
return result
361365

362366
def sync_render(self):
363-
return async_to_sync(super().render)()
367+
return self.thread.submit(self.loop.run_until_complete, self.render()).result()
364368

365369

366370
def get_pk(model):

0 commit comments

Comments
 (0)