Skip to content

fix(client): let a caller context reach the report upload - #60

Merged
matthyx merged 1 commit into
kubescape:mainfrom
ArneshBanerjee:fix/58-submit-report-context
Aug 25, 2026
Merged

fix(client): let a caller context reach the report upload#60
matthyx merged 1 commit into
kubescape:mainfrom
ArneshBanerjee:fix/58-submit-report-context

Conversation

@ArneshBanerjee

Copy link
Copy Markdown
Contributor

Fixes #58

SubmitReport takes no context, so the report POST always ran with context.Background(). Kubescape checks ctx.Err() between report chunks, but once an upload was on the wire nothing could stop it, and a cancelled scan or a shutdown had to wait out the client timeout.

This adds SubmitReportWithContext(ctx, report, opts...) and keeps SubmitReport as a wrapper over it with context.Background(), so existing callers are unchanged. The context goes through the WithContext request option that is already used by get/post, so no new plumbing was needed. This is the same shape as GetExceptionsWithContext and GetControlsInputsWithContext from #57.

Tests use a recording RoundTripper that can block until the request context is done, and cover:

  • the caller context reaching the transport
  • cancelling after the request has started, returning promptly with context.Canceled
  • a deadline surfacing as context.DeadlineExceeded
  • request options still applying alongside the context
  • SubmitReport behaving as before

Verified locally with go build ./..., go test ./... and go test -race ./..., all passing. go vet ./... reports four copylocks findings in pkg/server/v1/systemreports/datastructures_test.go, which are present on main before this change and are untouched here.

SubmitReport had no way to take a context, so the POST always ran with
context.Background(). Once an upload started, a cancelled scan or a
shutdown could not stop it and the caller waited for the client timeout.

Add SubmitReportWithContext, which passes the caller context through the
existing WithContext request option, and keep SubmitReport as a wrapper
over it using context.Background() so existing callers are unaffected.
This follows how GetExceptionsWithContext and GetControlsInputsWithContext
were added in kubescape#57.

Fixes kubescape#58

Signed-off-by: Arnesh Banerjee <linkrinku13@gmail.com>
@coderabbitai

coderabbitai Bot commented Aug 24, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 6e70556a-7a57-4c0f-9dd0-7834d8c854c4


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@matthyx matthyx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed against #58's acceptance criteria — all met:

  • SubmitReportWithContext(ctx, report, opts...) is additive; SubmitReport stays a thin wrapper over context.Background(), so existing callers are unaffected.
  • The context reaches the transport via the existing WithContext request option → http.NewRequestWithContext, same shape as GetExceptionsWithContext/GetControlsInputsWithContext from #57.
  • opts = append([]RequestOption{WithContext(ctx), WithContentJSON(true)}, opts...) preserves the original WithContentJSON(true) behavior for callers while still letting explicit opts win, consistent with the functional-options pattern in kscloudoption.go.
  • The recording RoundTripper tests are solid: they verify the context value reaches the request, that cancelling an in-flight (blocked) request surfaces context.Canceled, that a timeout surfaces context.DeadlineExceeded (both checked with errors.Is, so they survive the *url.Error wrapping from http.Client), that extra request options still apply, and that plain SubmitReport still works.
  • Checks are green (GitGuardian, DCO); the go vet copylocks findings mentioned in the description are pre-existing on main and untouched here.

No blockers. Approving.

@matthyx
matthyx merged commit b127e3d into kubescape:main Aug 25, 2026
4 checks passed
@matthyx matthyx moved this to To Archive in KS PRs tracking Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To Archive

Development

Successfully merging this pull request may close these issues.

client: report uploads cannot cancel an in-flight HTTP request

2 participants