Skip to content

Conversation

@yashwantbezawada
Copy link

  • I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

This PR fixes issue #2737 where the OpenAI client's broad exception handling was catching Celery's SoftTimeLimitExceeded exception and retrying the request instead of allowing it to propagate for graceful task shutdown.

Changes:

  • Added _should_not_retry() helper function to identify termination signals that should propagate immediately
  • Modified both sync and async exception handlers to check for termination signals before attempting retry
  • Added test coverage to verify termination signals are not retried

Affected exception types:

  • Celery task limits: SoftTimeLimitExceeded, TimeLimitExceeded, Terminated
  • Asyncio cancellation: CancelledError

The fix preserves all existing retry logic while ensuring task executor termination signals are properly handled.

Additional context & links

Fixes #2737

Background:
When using the OpenAI client in Celery tasks with soft time limits, if the task exceeds its limit during an API call, Celery raises SoftTimeLimitExceeded. The client's except Exception clause was catching this and treating it as a retryable connection error, preventing cleanup logic from running.

Solution approach:
Instead of narrowing the exception catch (which could miss legitimate connection errors), we check if caught exceptions are termination signals and re-raise them immediately without retry. This approach:

  • Maintains backward compatibility
  • Doesn't require importing Celery (checks by module name)
  • Handles both sync and async code paths
  • Is extensible for other task executors
@yashwantbezawada yashwantbezawada requested a review from a team as a code owner November 7, 2025 16:09
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines 114 to 116
# asyncio cancellation
if exc_module == "asyncio" and exc_name == "CancelledError":
return True

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Ensure asyncio cancellations bypass retry loop

_should_not_retry checks for CancelledError by requiring exc.__class__.__module__ == "asyncio", but asyncio.CancelledError is defined in asyncio.exceptions across supported Python versions. The predicate never matches, so cancelled tasks are still retried and eventually surface as APIConnectionError instead of propagating the cancellation. This defeats the intended behavior for async termination signals.

Useful? React with 👍 / 👎.

@yashwantbezawada
Copy link
Author

Thanks for the review! You're absolutely right - asyncio.CancelledError is in asyncio.exceptions, not asyncio directly.

I've updated the check to use startswith("asyncio") (consistent with the Celery check pattern) and added a test case specifically for asyncio.CancelledError to ensure it properly bypasses the retry loop.

Changes in bd96a86:

  • Fixed module check from == "asyncio" to startswith("asyncio")
  • Added test_asyncio_cancelled_error_not_retried test case
When using the OpenAI client in Celery tasks with soft time limits, the client's broad exception handling was catching SoftTimeLimitExceeded and treating it as a retryable connection error. This prevented Celery tasks from properly handling timeouts and running cleanup logic. This change adds a check to identify termination signals (like Celery's SoftTimeLimitExceeded or asyncio's CancelledError) and re-raises them immediately without retry. This allows task executors to properly handle these signals. Changes: - Added _should_not_retry() helper to identify termination signals - Modified sync and async exception handlers to check before retrying - Added test to verify termination signals are not retried Fixes openai#2737
- Fix module check to use startswith("asyncio") instead of == "asyncio" to properly match asyncio.exceptions.CancelledError - Add test case for asyncio.CancelledError to verify it bypasses retry loop Addresses review feedback from chatgpt-codex-connector bot
@yashwantbezawada yashwantbezawada force-pushed the fix/celery-soft-time-limit-exception branch from bd96a86 to 20e8fd0 Compare November 7, 2025 16:50
@yashwantbezawada
Copy link
Author

Rebased on latest main to bring branch up-to-date (was 10 commits behind).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants