Skip to content

fix(bitbucket): prevent TorngitServerUnreachableError in find_pull_request#1211

Open
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/bitbucket-unreachable-error
Open

fix(bitbucket): prevent TorngitServerUnreachableError in find_pull_request#1211
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/bitbucket-unreachable-error

Conversation

@sentry

@sentry sentry Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

This PR addresses the TorngitServerUnreachableError occurring in the app.tasks.notify.Notify task when interacting with Bitbucket.

Root Cause:
The find_pull_request function in shared/torngit/bitbucket.py was attempting to find a commit's associated pull request by paginating through all open pull requests for a repository. For repositories with a large number of open PRs (e.g., 25+ pages), this led to making 25+ sequential API calls on the same HTTP connection. Bitbucket's server has a keep-alive limit, causing the connection to be closed after approximately 25 requests. Subsequent requests on the same connection would then fail with a ReadError (specifically anyio.BrokenResourceError), which was broadly caught and re-raised as TorngitServerUnreachableError, masking the true cause of connection exhaustion.

Solution:
To resolve this, the find_pull_request function has been updated to leverage Bitbucket's commit-specific API endpoint (GET /repositories/{slug}/commit/{commit}/pullrequests) when searching for a pull request by commit SHA. This approach:

  1. Eliminates unbounded pagination: A single API call is made to retrieve PRs associated with a specific commit, avoiding the need to iterate through all open PRs.
  2. Prevents connection exhaustion: By reducing the number of sequential requests on a single HTTP connection, the issue of Bitbucket closing the connection due to keep-alive limits is circumvented.
  3. Mirrors GitHub's implementation: This change aligns the Bitbucket integration with the more efficient method already used for GitHub.

The existing pagination logic is retained only for the less common case of finding a pull request by branch name, where a commit-specific shortcut is not available.

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. In 2022 this entity acquired Codecov and as result Sentry is going to need some rights from me in order to utilize my contributions in this PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.

Fixes WORKER-YTF

@codspeed-hq

codspeed-hq Bot commented Jun 19, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 9 untouched benchmarks


Comparing seer/fix/bitbucket-unreachable-error (703e017) with main (ee7a2b4)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (7b60783) during the generation of this report, so ee7a2b4 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@codecov

codecov Bot commented Jun 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.91%. Comparing base (7b60783) to head (703e017).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1211      +/-   ##
==========================================
+ Coverage   91.89%   91.91%   +0.01%     
==========================================
  Files        1325     1325              
  Lines       50862    50853       -9     
  Branches     1626     1620       -6     
==========================================
- Hits        46741    46740       -1     
+ Misses       3815     3808       -7     
+ Partials      306      305       -1     
Flag Coverage Δ
apiunit 94.95% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@codecov-notifications

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

0 participants