Skip to content

fix(backend): handle Gitea ERR_STREAM_PREMATURE_CLOSE during sync#1405

Merged
brendan-kellam merged 4 commits into
mainfrom
brendan/sou-1484-bug-gitea-sync-fails-with-err_stream_premature_close-75d2
Jul 8, 2026
Merged

fix(backend): handle Gitea ERR_STREAM_PREMATURE_CLOSE during sync#1405
brendan-kellam merged 4 commits into
mainfrom
brendan/sou-1484-bug-gitea-sync-fails-with-err_stream_premature_close-75d2

Conversation

@brendan-kellam

@brendan-kellam brendan-kellam commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Fixes SOU-1484
Fixes #1404

Syncing repos from some self-hosted Gitea instances fails when cross-fetch can't read the API response body (ERR_STREAM_PREMATURE_CLOSE). repoGet() then returns { data: null, error: {...} }, the null gets pushed into the repo list, and the sync crashes on repo.full_name.

Changes:

  • Wrap the Gitea fetch to force Accept-Encoding: identity and Connection: close, which avoids the premature close (root cause).
  • Throw on an errored/empty repoGet response so a failed fetch surfaces as a warning/error instead of a silent null.
  • Guard the repo filter against null/undefined entries.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed Gitea sync failures by improving response-stream handling during API fetches.
    • Improved repository syncing to skip invalid, empty, or null repository entries.
    • Added safer validation when fetching repository details to prevent failures from missing or erroneous responses.
  • Documentation
    • Updated the changelog with the Gitea sync-failure fix.

Force identity encoding / connection close in the Gitea API fetch to avoid
cross-fetch failing to read the response body, and guard against null repo
data so a failed fetch no longer crashes on repo.full_name.

Co-authored-by: linear-code[bot] <222613912+linear-code[bot]@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e31f4cc4-be1d-4596-87cb-f5033de35c11

📥 Commits

Reviewing files that changed from the base of the PR and between 474b7be and d3e5d28.

📒 Files selected for processing (1)
  • CHANGELOG.md

Walkthrough

This PR changes Gitea sync to force identity-encoded fetches, reject invalid repository responses, and skip null repository entries. It also adds a changelog entry for the fix.

Changes

Gitea sync reliability fix

Layer / File(s) Summary
Custom fetch wrapper and client wiring
packages/backend/src/gitea.ts
Introduces a customFetch wrapper that sets Accept-Encoding: identity and Connection: close, and uses it in giteaApi.
Response validation and null repo filtering
packages/backend/src/gitea.ts, CHANGELOG.md
Throws on repoGet responses with errors or missing data, filters null/undefined repositories with a warning, and records the fix in the changelog.

Estimated code review effort: 2 (Simple) | ~12 minutes

Sequence Diagram(s)

sequenceDiagram
  participant SyncJob
  participant giteaApi
  participant customFetch
  participant GiteaServer

  SyncJob->>giteaApi: repoGet(org, repo)
  giteaApi->>customFetch: fetch request
  customFetch->>GiteaServer: HTTP request (Accept-Encoding identity, Connection close)
  GiteaServer-->>customFetch: response body
  customFetch-->>giteaApi: response
  giteaApi-->>SyncJob: HttpResponse(data, error)
  alt error present or data missing
    SyncJob->>SyncJob: throw error
  else valid data
    SyncJob->>SyncJob: filter null/undefined repos, log warning
    SyncJob->>SyncJob: continue aggregation
  end
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main fix: handling Gitea premature-close sync failures in the backend.
Linked Issues check ✅ Passed The changes address the linked bug by hardening fetch, rejecting null repoGet responses, and guarding null repo entries.
Out of Scope Changes check ✅ Passed The PR stays focused on the Gitea sync failure and adds only a changelog note alongside the code fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch brendan/sou-1484-bug-gitea-sync-fails-with-err_stream_premature_close-75d2

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.

Co-authored-by: linear-code[bot] <222613912+linear-code[bot]@users.noreply.github.com>
@brendan-kellam brendan-kellam marked this pull request as ready for review June 30, 2026 23:02

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/backend/src/gitea.ts`:
- Around line 230-233: The paginated repo listing paths in
getReposOwnedByUsers() and getReposForOrgs() are missing the same
empty/error-response guard used elsewhere, so a premature-close from paginate()
can bubble up as an opaque failure. Update the pagination handling in these
functions in gitea.ts to explicitly check for response.error or missing
response.data after each page fetch, and throw a clear error with the relevant
repo/org context before mapping or accumulating results.
- Around line 15-29: The custom fetch wrapper in gitea.ts is forcing Connection:
close for every request, which breaks keep-alive reuse during paginated
org/repo/user syncs. Update customFetch to apply this workaround only for
affected Gitea hosts or error-prone cases, or switch to a keep-alive agent while
still preserving the Accept-Encoding identity workaround. Keep the change
localized to customFetch so the rest of the Gitea request flow continues using
normal connection reuse.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 128bae05-8209-4db2-b9bc-93dcd60bd6ba

📥 Commits

Reviewing files that changed from the base of the PR and between 216c7d8 and 474b7be.

📒 Files selected for processing (2)
  • CHANGELOG.md
  • packages/backend/src/gitea.ts

Comment thread packages/backend/src/gitea.ts
Comment thread packages/backend/src/gitea.ts
@brendan-kellam brendan-kellam merged commit 9f42c66 into main Jul 8, 2026
6 of 7 checks passed
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.

[bug] Gitea sync fails with ERR_STREAM_PREMATURE_CLOSE, then crashes on null full_name

1 participant