Skip to content

fix: replace 11 bare except clauses with except Exception#444

Closed
haosenwang1018 wants to merge 1 commit into
celery:mainfrom
haosenwang1018:fix/bare-excepts
Closed

fix: replace 11 bare except clauses with except Exception#444
haosenwang1018 wants to merge 1 commit into
celery:mainfrom
haosenwang1018:fix/bare-excepts

Conversation

@haosenwang1018

Copy link
Copy Markdown

Replace bare except clauses with except Exception.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR replaces several bare except: clauses in billiard with except Exception: in an effort to avoid catching BaseException subclasses (e.g., KeyboardInterrupt, SystemExit).

Changes:

  • Replace bare except: with except Exception: across multiple process/IPC-related modules.
  • Narrow exception handling in resource/handle cleanup and background service loops.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
billiard/semaphore_tracker.py Narrows exception handlers in tracker startup and exception-reporting path.
billiard/resource_sharer.py Narrows the server loop’s catch-all handler.
billiard/process.py Narrows _bootstrap() outermost catch-all handler.
billiard/popen_spawn_win32.py Narrows Windows handle cleanup catch-all handler during process spawn.
billiard/managers.py Narrows exception handling during manager shutdown response.
billiard/forkserver.py Narrows fd cleanup handlers in forkserver connection/spawn logic.
billiard/connection.py Narrows overlapped I/O cleanup handlers on Windows pipe operations.
billiard/compat.py Narrows sysconf failure handling in get_fdmax.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread billiard/connection.py
Comment on lines +318 to 320
except Exception:
ov.cancel()
raise

Copilot AI Feb 26, 2026

Copy link

Choose a reason for hiding this comment

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

This except ensures ov.cancel() runs if the wait/write fails. With except Exception, KeyboardInterrupt/SystemExit during the wait will bypass cancellation, potentially leaving an overlapped I/O pending and leaking resources. Use except BaseException for the cancel-and-reraise pattern here.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can you please cross check this suggestion?

Comment thread billiard/connection.py
Comment thread billiard/semaphore_tracker.py
Comment thread billiard/semaphore_tracker.py
Comment thread billiard/process.py
Comment on lines +336 to 338
except Exception:
exitcode = 1
if not util.error('Process %s', self.name, exc_info=True):

Copilot AI Feb 26, 2026

Copy link

Choose a reason for hiding this comment

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

exitcode is only set inside the try/except blocks. With except Exception, a KeyboardInterrupt/SystemExit raised anywhere in the outer try will skip the handlers and hit finally, where exitcode is referenced, causing an UnboundLocalError and masking the real reason for exit. Either initialize exitcode before the try and/or handle BaseException to ensure exitcode is always defined.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can you please cross check this @haosenwang1018

Comment thread billiard/popen_spawn_win32.py
Comment thread billiard/forkserver.py
reduction.sendfds(client, allfds)
return parent_r, parent_w
except:
except Exception:

Copilot AI Feb 26, 2026

Copy link

Choose a reason for hiding this comment

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

This block is closing parent_r/parent_w on failure, but except Exception won’t run for KeyboardInterrupt/SystemExit, which can leak both fds. Use except BaseException for cleanup-and-reraise here, or move the closes to a finally that runs for all exception types when returning doesn’t happen.

Suggested change
except Exception:
except BaseException:

Copilot uses AI. Check for mistakes.
Comment thread billiard/forkserver.py

@Nusnus Nusnus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@copilot code review[agent] comments seems legit. Please review @haosenwang1018

@haosenwang1018

Copy link
Copy Markdown
Author

Closing broad-scope change per backlog policy; can resubmit in smaller chunks.

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.

4 participants