Skip to content

Commit

Permalink
🐛 fix none driver
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Oct 22, 2024
1 parent 56ac77e commit e9ca078
Showing 1 changed file with 32 additions and 40 deletions.
72 changes: 32 additions & 40 deletions nonebot/drivers/none.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,7 @@ def run(self, *args, **kwargs):
async def _serve(self):
async with anyio.create_task_group() as driver_tg:
driver_tg.start_soon(self._handle_signals)

# wait for startup complete or exit
async with anyio.create_task_group() as start_tg:
start_tg.start_soon(self._listen_exit, start_tg)

await self._startup()
start_tg.cancel_scope.cancel()

if self.should_exit.is_set():
return

await self._listen_exit()

await self._shutdown()

driver_tg.cancel_scope.cancel()
driver_tg.start_soon(self._handle_lifespan, driver_tg)

Check warning on line 64 in nonebot/drivers/none.py

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L64

Added line #L64 was not covered by tests

async def _handle_signals(self):

Check warning on line 66 in nonebot/drivers/none.py

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L66

Added line #L66 was not covered by tests
try:
Expand All @@ -92,6 +77,20 @@ async def _handle_signals(self):
def _handle_legacy_signal(self, sig, frame):
self.exit(force=self.should_exit.is_set())

async def _handle_lifespan(self, tg: TaskGroup):

Check warning on line 80 in nonebot/drivers/none.py

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L80

Added line #L80 was not covered by tests
try:
await self._startup()

if self.should_exit.is_set():

Check warning on line 84 in nonebot/drivers/none.py

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L83-L84

Added lines #L83 - L84 were not covered by tests
return

await self._listen_exit()

Check warning on line 87 in nonebot/drivers/none.py

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L86-L87

Added lines #L86 - L87 were not covered by tests

tg.start_soon(self._listen_force_exit, tg)

Check warning on line 89 in nonebot/drivers/none.py

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L89

Added line #L89 was not covered by tests
await self._shutdown()
finally:
tg.cancel_scope.cancel()

Check warning on line 92 in nonebot/drivers/none.py

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L91-L92

Added lines #L91 - L92 were not covered by tests

async def _startup(self):

Check warning on line 94 in nonebot/drivers/none.py

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L94

Added line #L94 was not covered by tests
def handle_exception(exc_group: BaseExceptionGroup[Exception]) -> None:
self.should_exit.set()
Expand Down Expand Up @@ -120,37 +119,30 @@ async def _listen_exit(self, tg: Optional[TaskGroup] = None):

Check warning on line 119 in nonebot/drivers/none.py

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L119

Added line #L119 was not covered by tests
async def _shutdown(self):
logger.info("Shutting down")
logger.info("Waiting for application shutdown. (CTRL+C to force quit)")

# wait for shutdown complete or force exit
async with anyio.create_task_group() as tg:
tg.start_soon(self._listen_force_exit, tg)
error_occurred: bool = False

Check warning on line 124 in nonebot/drivers/none.py

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L124

Added line #L124 was not covered by tests

logger.info("Waiting for application shutdown. (CTRL+C to force quit)")

error_occurred: bool = False

def handle_exception(exc_group: BaseExceptionGroup[Exception]) -> None:
nonlocal error_occurred
def handle_exception(exc_group: BaseExceptionGroup[Exception]) -> None:

Check warning on line 126 in nonebot/drivers/none.py

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L126

Added line #L126 was not covered by tests
nonlocal error_occurred

error_occurred = True
error_occurred = True

for exc in flatten_exception_group(exc_group):
logger.opt(colors=True, exception=exc).error(
"<r><bg #f8bbd0>Error occurred while running shutdown hook."
"</bg #f8bbd0></r>"
)
logger.error(
"<r><bg #f8bbd0>Application shutdown failed. "
"Exiting.</bg #f8bbd0></r>"
for exc in flatten_exception_group(exc_group):

Check warning on line 131 in nonebot/drivers/none.py

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L131

Added line #L131 was not covered by tests
logger.opt(colors=True, exception=exc).error(
"<r><bg #f8bbd0>Error occurred while running shutdown hook."
"</bg #f8bbd0></r>"

Check warning on line 134 in nonebot/drivers/none.py

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L133-L134

Added lines #L133 - L134 were not covered by tests
)
logger.error(
"<r><bg #f8bbd0>Application shutdown failed. "
"Exiting.</bg #f8bbd0></r>"

Check warning on line 138 in nonebot/drivers/none.py

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L138

Added line #L138 was not covered by tests
)

with catch({Exception: handle_exception}):
await self._lifespan.shutdown()

if not error_occurred:
logger.info("Application shutdown complete.")
with catch({Exception: handle_exception}):
await self._lifespan.shutdown()

tg.cancel_scope.cancel()
if not error_occurred:

Check warning on line 144 in nonebot/drivers/none.py

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L144

Added line #L144 was not covered by tests
logger.info("Application shutdown complete.")

async def _listen_force_exit(self, tg: TaskGroup):

Check warning on line 147 in nonebot/drivers/none.py

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L147

Added line #L147 was not covered by tests
await self.force_exit.wait()
Expand Down

0 comments on commit e9ca078

Please sign in to comment.