Skip to content

Python SDK: a fork in a signal handler during a prompt fetch makes the child call wait for the fetch timeout #107

Closed

Description

@ephraimduncan

The fix of #98 (#105) resets pending prompt fetches in a forked child. It does not cover one case: a fork in a signal handler while a get_prompt call waits for a fetch.

The call waits on the event of the fetch in _prompts.py. After the fork, the child call continues to wait on the copied event, but no thread in the child sets that event. Thus the child call waits until the fetch timeout (up to 10 s) and then raises a timeout error, although the server answers at once.

Repro: with _FETCH_TIMEOUT_S = 2 and a server delay of 1 s, a call waits for a fetch, and a SIGALRM handler calls os.fork() 0.3 s later. The parent gets the prompt in 1.00 s. The child raises PromptError('Fetching the prompt took more than 2 s') after 2.00 s. This occurred on Python 3.10, 3.12, and 3.13 on Linux.

What to do:

  1. After the wait on a pending event, compare the process ID with the ID of the fetch.
  2. If the process ID changed, start a new fetch in the child.
  3. Add a real-fork test with a signal handler, as in the repro.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions