Skip to content

Fix APK handler spinning on closed temp file after context timeout#4870

Draft
dipto-truffle wants to merge 1 commit intomainfrom
fix/apk-handler-context-cancellation
Draft

Fix APK handler spinning on closed temp file after context timeout#4870
dipto-truffle wants to merge 1 commit intomainfrom
fix/apk-handler-context-cancellation

Conversation

@dipto-truffle
Copy link
Copy Markdown
Contributor

@dipto-truffle dipto-truffle commented Apr 6, 2026

Summary

  • The APK handler's processAPK loop in pkg/handlers/apk.go did not check for context cancellation, causing it to iterate through every file in a zip archive even after the 60s maxTimeout fires and the underlying temp file is closed. This produced ~2000 "file already closed" error log entries per APK and wasted scanner CPU/IO, contributing to slow scans for co-located jobs.
  • Added a common.IsDone(ctx) check at the top of the file iteration loop, matching the established pattern already used by archive.go, ar.go, and rpm.go in the same package.
  • Added unit tests verifying that processAPK exits promptly on both context cancellation and deadline exceeded.

Vendors trufflesecurity/thog#6007

Test plan

  • go test ./pkg/handlers/ -run TestProcessAPK_ExitsOn -v passes (context cancellation + deadline exceeded)
  • Deploy to a dev environment and verify that APK processing no longer produces thousands of "file already closed" log entries after timeout
  • Confirm scan throughput is not degraded

Made with Cursor


Note

Low Risk
Low risk: adds an early-exit context check in the APK file iteration loop and corresponding unit tests, with minimal impact on normal processing aside from stopping work sooner on cancellation/timeouts.

Overview
Ensures APK scanning stops promptly when the processing context is cancelled or times out by adding a common.IsDone(ctx) guard in apkHandler.processAPK before iterating each zip entry.

Adds unit tests that build a minimal in-memory APK and assert processAPK returns context.Canceled and context.DeadlineExceeded rather than continuing to process files after cancellation.

Reviewed by Cursor Bugbot for commit f7c44b4. Bugbot is set up for automated code reviews on this repo. Configure here.

The APK handler's processAPK loop did not check for context
cancellation, causing it to iterate through every file in a zip archive
even after the 60s maxTimeout fires and the underlying temp file is
closed. This produces thousands of "file already closed" error logs per
APK and wastes scanner resources, starving co-located scan jobs.

Add a common.IsDone(ctx) check at the top of the file iteration loop,
matching the established pattern in archive.go, ar.go, and rpm.go.

Made-with: Cursor
@dipto-truffle dipto-truffle force-pushed the fix/apk-handler-context-cancellation branch from 4e68573 to f7c44b4 Compare April 6, 2026 20:12
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.

1 participant