larksuite / larksuite/oapi-sdk-python

FeishuChannel.stop leaves bot identity retry task pending

Open Beginner friendly
#159 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
559
Forks
102
PR merge metrics
No merged PRs in 30d

Description

Affected version

  • lark-oapi 1.7.3
  • Default branch commit: 0b9e6e48b74bb4b34462fc67b7e738b27e73e697
  • Reproduced on Python 3.13.13 and 3.14.4 on Windows

Problem

FeishuChannel._start_bot_identity_retry_loop() submits _bot_identity_retry_loop() with asyncio.run_coroutine_threadsafe() but discards the returned future. Unlike FeishuChannel.schedule(), this path never adds the future to _bg_tasks.

FeishuChannel.stop() only cancels futures in _bg_tasks before stopping and closing the background event loop. A sleeping bot-identity retry therefore survives shutdown as a pending task.

Minimal reproduction

import asyncio
import time
from unittest.mock import patch

from lark_oapi.channel import FeishuChannel

channel = FeishuChannel(app_id="cli_x", app_secret="s")
channel._ensure_bg_loop()
channel._BOT_IDENTITY_RETRY_DELAYS_S = (3600,)

captured = []
original = asyncio.run_coroutine_threadsafe

def capture(coro, loop):
    future = original(coro, loop)
    captured.append(future)
    return future

with patch(
    "lark_oapi.channel.channel.asyncio.run_coroutine_threadsafe",
    side_effect=capture,
):
    channel._start_bot_identity_retry_loop()

time.sleep(0.05)
print(captured[0] in channel._bg_tasks)  # False
channel.stop()
print(captured[0].done(), captured[0].cancelled())  # False False

At interpreter shutdown:

Task was destroyed but it is pending!
task: <Task pending ... coro=<FeishuChannel._bot_identity_retry_loop() ...>>

The same pending-task warning appeared after the native test suite on both Python 3.13 and 3.14.

Expected behavior

stop() should cancel and drain every task owned by the channel background loop, including the bot-identity retry task, without a pending-task warning.

Impact

A startup identity lookup failure schedules a retry that can sleep for up to an hour. Stopping or reconnecting the channel during that delay closes its loop while the retry is still pending. This produces noisy shutdown diagnostics and leaves channel-owned work outside the documented lifecycle tracking.

Suggested implementation

Route this coroutine through the existing schedule() helper, or explicitly track its future in _bg_tasks, then add a lifecycle regression test that starts a long-delay retry, calls stop(), and verifies the future is cancelled and no pending task remains.

I searched open and closed issues, all pull request states, and repository history for bot identity retry, Task was destroyed, _start_bot_identity_retry_loop, and related lifecycle terms. I did not find an existing report or competing fix. Open PRs #139, #144, and #151 touch adjacent client/channel lifecycle code but do not track or cancel this retry future.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lark_oapi.channel.channel with _start_bot_identity_retry_loop(), schedule(), and stop(). Reproduce the long-delay retry, then add a lifecycle regression test that verifies stop cancels and drains the retry future without a pending-task warning. Done means the bot-identity retry is tracked by the channel and shutdown leaves no pending task.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend-api-design
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.