Skip to content

Commit

Permalink
Fix prerender SynchronousOnlyOperation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Archmonger committed Dec 28, 2024
1 parent 5341851 commit 3e9a1fb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ Don't forget to remove deprecated code on each major release!

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

### Fixed

- Fixed bug where prerendered components could generate a `SynchronousOnlyOperation` exception if they utilize the Django ORM.

## [5.1.1] - 2024-12-02

### Fixed
Expand Down
16 changes: 10 additions & 6 deletions src/reactpy_django/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import asyncio
import contextlib
import inspect
import logging
Expand All @@ -16,7 +17,6 @@
from uuid import UUID, uuid4

import dill
from asgiref.sync import async_to_sync
from channels.db import database_sync_to_async
from django.contrib.staticfiles.finders import find
from django.core.cache import caches
Expand Down Expand Up @@ -353,14 +353,18 @@ class SyncLayout(Layout):
"""

def __enter__(self):
async_to_sync(self.__aenter__)()
return self
self.loop = asyncio.new_event_loop()
self.thread = ThreadPoolExecutor(max_workers=1)
return self.thread.submit(self.loop.run_until_complete, self.__aenter__()).result()

def __exit__(self, *_):
async_to_sync(self.__aexit__)(*_)
def __exit__(self, *exec):
result = self.thread.submit(self.loop.run_until_complete, self.__aexit__(*exec)).result()
self.loop.close()
self.thread.shutdown()
return result

def sync_render(self):
return async_to_sync(super().render)()
return self.thread.submit(self.loop.run_until_complete, self.render()).result()


def get_pk(model):
Expand Down

0 comments on commit 3e9a1fb

Please sign in to comment.