Skip to content

Recover from panics in library goroutines - #235

Merged
andrew merged 1 commit into
mainfrom
recover-library-goroutines
Jun 21, 2026
Merged

andrew merged 1 commit into
mainfrom
recover-library-goroutines

Conversation

@andrew

@andrew andrew commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

When git-pkgs is embedded as a library (the upcoming silo server), a caller can wrap Indexer.Run in a recover() to survive a panic on hostile input, but that does not cover goroutines spawned inside the call.

There are exactly two such goroutines in non-test, non-cmd code:

internal/indexer/indexer.go collects tags and branches via go-git concurrently with the commit walk. go-git is iterating attacker-controlled refs and packfiles here, so a panic in that path would kill the host process and leave the receiver blocked on refCh. The send is moved into a defer that swallows any panic; r is initialised before the defer is registered so the receiver always gets a valid (possibly partial) result.

internal/database/batch_writer.go runs flushPending in the background during FlushAsync. A panic there now surfaces as an error on flushDone so WaitForFlush returns it instead of hanging.

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 hardens git-pkgs when embedded as a library by ensuring panics in internal background goroutines don’t crash the host process or cause the caller to hang waiting on a channel result.

Changes:

  • In the indexer’s refs-collection goroutine, always send a (possibly partial) refs result on refCh, even if go-git panics while iterating refs/packfiles.
  • In the database batch writer’s async flush goroutine, recover from panics and surface them as an error so WaitForFlush returns instead of hanging.

Reviewed changes

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

File Description
internal/indexer/indexer.go Wrap ref collection goroutine with a defer that recovers panics and guarantees a send on refCh.
internal/database/batch_writer.go Add panic recovery to async flush goroutine and convert panics into errors delivered via flushDone.

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

@andrew
andrew merged commit 633d302 into main Jun 21, 2026
8 checks passed
@andrew
andrew deleted the recover-library-goroutines branch June 21, 2026 17:07
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