Skip to content

Commit

Permalink
🐛 fix bot connect when startup
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Oct 24, 2024
1 parent 83a4127 commit 29df9db
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion nonebot/drivers/none.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +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)
driver_tg.start_soon(self._listen_force_exit, driver_tg)
driver_tg.start_soon(self._handle_lifespan, driver_tg)

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

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L64-L65

Added lines #L64 - L65 were not covered by tests

async def _handle_signals(self):

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

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L67

Added line #L67 was not covered by tests
Expand All @@ -86,7 +87,6 @@ async def _handle_lifespan(self, tg: TaskGroup):

await self._listen_exit()

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

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L87-L88

Added lines #L87 - L88 were not covered by tests

tg.start_soon(self._listen_force_exit, tg)
await self._shutdown()

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

View check run for this annotation

Codecov / codecov/patch

nonebot/drivers/none.py#L90

Added line #L90 was not covered by tests
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#L92

Added line #L92 was not covered by tests
Expand Down
16 changes: 8 additions & 8 deletions nonebot/internal/driver/_lifespan.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ async def _run_lifespan_func(
await run_sync(cast(SYNC_LIFESPAN_FUNC, func))()

async def startup(self) -> None:
# run startup funcs
if self._startup_funcs:
await self._run_lifespan_func(self._startup_funcs)

# create background task group
self.task_group = anyio.create_task_group()
await self.task_group.__aenter__()

# run startup funcs
if self._startup_funcs:
await self._run_lifespan_func(self._startup_funcs)

# run ready funcs
if self._ready_funcs:
await self._run_lifespan_func(self._ready_funcs)
Expand All @@ -76,6 +76,10 @@ async def shutdown(
exc_val: Optional[BaseException] = None,
exc_tb: Optional[TracebackType] = None,
) -> None:
if self._shutdown_funcs:
# reverse shutdown funcs to ensure stack order
await self._run_lifespan_func(reversed(self._shutdown_funcs))

# shutdown background task group
self.task_group.cancel_scope.cancel()

Expand All @@ -84,10 +88,6 @@ async def shutdown(

self._task_group = None

if self._shutdown_funcs:
# reverse shutdown funcs to ensure stack order
await self._run_lifespan_func(reversed(self._shutdown_funcs))

async def __aenter__(self) -> None:
await self.startup()

Expand Down

0 comments on commit 29df9db

Please sign in to comment.