Skip to content

Add CodeQL query to detect unclosed Repository and Storage instances#8

Merged
pjbgf merged 2 commits into
go-git:mainfrom
AriehSchneier:add-codeql-resource-leak-detection
May 7, 2026
Merged

Add CodeQL query to detect unclosed Repository and Storage instances#8
pjbgf merged 2 commits into
go-git:mainfrom
AriehSchneier:add-codeql-resource-leak-detection

Conversation

@AriehSchneier
Copy link
Copy Markdown
Contributor

@AriehSchneier AriehSchneier commented May 7, 2026

Summary

Implements static analysis to detect resource leaks in go-git usage where Repository or Storage instances are created but never closed. This provides compile-time detection of file handle leaks as an alternative to runtime leak detection.

What's included

CodeQL Query (codeql/queries/unclosed-resources.ql)

Detects patterns where resources are created but not closed:

  • Repository creation via PlainOpen, Init, Clone, and related functions
  • Storage creation via NewStorage and NewStorageWithOptions
  • Submodule and worktree operations that return repositories
  • Missing Close() calls or defer cleanup patterns

The query excludes false positives by ignoring:

  • Resources stored in fields (managed elsewhere)
  • Resources returned to caller (caller's responsibility)

CodeQL Configuration (codeql/codeql-config.yml, codeql/qlpack.yml)

  • Package definition for the CodeQL query pack
  • Configuration file that references the custom query

GitHub Actions Workflow (.github/workflows/codeql.yml)

  • Runs automatically on pushes, PRs, and weekly schedule
  • Can be manually triggered with custom go-git branch/ref for testing
  • Uses latest actions: actions/checkout@v6.0.2 and codeql-action@v4.35.3
  • Analyzes go-git repository with the custom queries from this repo

Documentation (codeql/README.md)

  • Explains what the query detects
  • Provides good/bad examples
  • Documents how to run queries locally and in CI

Benefits

✅ Catches resource leaks at code review time, not test time
✅ No build tags or test infrastructure needed
✅ Works on all code, not just test code
✅ No runtime overhead
✅ Helps enforce best practices across the ecosystem

Test plan

  • Verify CodeQL query syntax is valid
  • Test workflow runs successfully
  • Manually trigger workflow against a go-git branch with known leaks
  • Confirm query detects the expected issues

🤖 Generated with Claude Code

@AriehSchneier AriehSchneier force-pushed the add-codeql-resource-leak-detection branch 5 times, most recently from fcbb6b2 to 1efa082 Compare May 7, 2026 15:11
Implements static analysis to detect resource leaks in go-git usage where
Repository or Storage instances are created but never closed. This provides
compile-time detection of file handle leaks as an alternative to runtime
leak detection.

The query detects:
- Repository creation via PlainOpen, Init, Clone, and related functions
- Storage creation via NewStorage and NewStorageWithOptions
- Submodule and worktree operations that return repositories
- Missing Close() calls or defer cleanup patterns

Includes a GitHub Actions workflow that:
- Runs on pushes, PRs, and weekly schedule
- Can be manually triggered with custom go-git branch/ref
- Uses latest actions/checkout@v6.0.2 and codeql-action@v4.35.3

Assisted-by: Claude Sonnet 4.5 <noreply@anthropic.com>
Signed-off-by: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com>
@AriehSchneier AriehSchneier force-pushed the add-codeql-resource-leak-detection branch from 1efa082 to 170d798 Compare May 7, 2026 15:13
Comment thread .github/workflows/codeql.yml Outdated
Comment thread .github/workflows/codeql.yml Outdated
Comment thread .github/workflows/codeql.yml Outdated
Co-authored-by: Paulo Gomes <paulo.gomes.uk@gmail.com>
Signed-off-by: Paulo Gomes <paulo.gomes.uk@gmail.com>
Copy link
Copy Markdown
Member

@pjbgf pjbgf left a comment

Choose a reason for hiding this comment

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

@AriehSchneier Thanks for working on this. 🙇

@pjbgf pjbgf merged commit bb2e8cd into go-git:main May 7, 2026
6 checks passed
@AriehSchneier AriehSchneier deleted the add-codeql-resource-leak-detection branch May 7, 2026 23:17
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