Skip to content

Ensure wakeup() is called even when add_job() raises an exception#1107

Closed
nuglifeleoji wants to merge 2 commits into
agronholm:3.xfrom
nuglifeleoji:fix/add-job-wakeup-on-error
Closed

Ensure wakeup() is called even when add_job() raises an exception#1107
nuglifeleoji wants to merge 2 commits into
agronholm:3.xfrom
nuglifeleoji:fix/add-job-wakeup-on-error

Conversation

@nuglifeleoji
Copy link
Copy Markdown

Problem (fixes #1092)

If the job store's add_job() call raises an unexpected exception (e.g. a SQLAlchemy error during a database write), _real_add_job() propagates the exception before reaching the self.wakeup() call at the bottom of the method.

# From _real_add_job():
try:
    store.add_job(job)          # <-- raises here
except ConflictingIdError:
    ...

# ... mark job, dispatch event, log ...

if self.state == STATE_RUNNING:
    self.wakeup()               # <-- never reached on unexpected exception

This leaves the scheduler's event loop without a wakeup signal. Already-scheduled, unrelated jobs stop being triggered until the scheduler is manually woken up.

Fix

Wrap the body of _real_add_job in a try/finally so that wakeup() is always called when the scheduler is STATE_RUNNING, regardless of whether add_job() succeeded or failed.

The job-added event dispatch and log message are left inside the try block and are therefore only emitted on success — the semantics of those operations are unchanged.

try:
    try:
        store.add_job(job)
    except ConflictingIdError:
        if replace_existing:
            store.update_job(job)
        else:
            raise
    # mark job, dispatch event, log ...
finally:
    if self.state == STATE_RUNNING:
        self.wakeup()

Made with Cursor

If the job store's add_job() call raised an unexpected exception (e.g. a
SQLAlchemy error), _real_add_job() would propagate the exception before
reaching the `self.wakeup()` call at the end of the method. This left the
scheduler's event loop without a wakeup signal, preventing already-scheduled
jobs from being triggered.

Move wakeup() into a `finally` block so it is always called when the
scheduler is in STATE_RUNNING, regardless of whether the add_job() call
succeeded or failed. The job-added event and log message are still only
emitted on success (they remain inside the try block).

Fixes #1092

Made-with: Cursor
@agronholm
Copy link
Copy Markdown
Owner

Why should the scheduler be woken up if adding the job failed? The whole premise seems odd to me. If the job store raises an exception from add_job(), then there is either a catastrophic failure or a bug in the job store. Either way, it is not the scheduler's job to catch such errors.

@nuglifeleoji nuglifeleoji force-pushed the fix/add-job-wakeup-on-error branch from 32bdcba to 34a46c2 Compare April 4, 2026 15:52
@agronholm
Copy link
Copy Markdown
Owner

Judging by the fact that you force-pushed a revert which makes this PR a no-op, I conclude that you were not serious about this.

@agronholm agronholm closed this Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants