Skip to content

Commit

Permalink
Silence runtime errors for ephemeral shutdown (#14163)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlowin committed Jun 20, 2024
1 parent a6b5dae commit f8cb3a6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/prefect/server/api/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,16 @@ async def create_events(
received_events = [event.receive() for event in events]
if ephemeral_request:
async with db.session_context() as session:
await database.write_events(session, received_events)
try:
await database.write_events(session, received_events)
except RuntimeError as exc:
if "can't create new thread at interpreter shutdown" in str(exc):
# Background events sometimes fail to write when the interpreter is shutting down.
# This is a known issue in Python 3.12.2 that can be ignored and is fixed in Python 3.12.3.
# see e.g. https://github.com/python/cpython/issues/113964
logger.debug("Received event during interpreter shutdown, ignoring")
else:
raise
else:
await messaging.publish(received_events)

Expand Down
5 changes: 4 additions & 1 deletion src/prefect/server/models/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from sqlalchemy.ext.asyncio import AsyncSession

import prefect.server.schemas as schemas
from prefect.logging import get_logger
from prefect.server.database.dependencies import inject_db
from prefect.server.database.interface import PrefectDBInterface
from prefect.utilities.collections import batched_iterable
Expand All @@ -22,6 +23,8 @@
# ...so we can only INSERT batches of a certain size at a time
LOG_BATCH_SIZE = MAXIMUM_QUERY_PARAMETERS // NUMBER_OF_LOG_FIELDS

logger = get_logger(__name__)


def split_logs_into_batches(logs):
for batch in batched_iterable(logs, LOG_BATCH_SIZE):
Expand Down Expand Up @@ -51,7 +54,7 @@ async def create_logs(
# Background logs sometimes fail to write when the interpreter is shutting down.
# This is a known issue in Python 3.12.2 that can be ignored and is fixed in Python 3.12.3.
# see e.g. https://github.com/python/cpython/issues/113964
pass
logger.debug("Received event during interpreter shutdown, ignoring")
else:
raise

Expand Down

0 comments on commit f8cb3a6

Please sign in to comment.