Skip to content

Fix unbound stream variable in DHT provider_store.py#1313

Merged
acul71 merged 3 commits into
libp2p:mainfrom
TimSC:provider_store
May 17, 2026
Merged

Fix unbound stream variable in DHT provider_store.py#1313
acul71 merged 3 commits into
libp2p:mainfrom
TimSC:provider_store

Conversation

@TimSC
Copy link
Copy Markdown
Contributor

@TimSC TimSC commented Apr 20, 2026

Fixes #1335

What was wrong?

When host.new_stream fails before a stream is opened, _send_add_provider hit an unbound stream in finally, masking the real error:

Error sending ADD_PROVIDER to <peer>: cannot access local variable 'stream' where it is not associated with a value

How was it fixed?

Initialize stream = None and only close when a stream was opened.

Test plan

  • Unit test: new_stream failure returns False without UnboundLocalError
  • make pr
  • make linux-docs

Cute Animal Picture

Zebrasoma desjardinii

Fixes libp2p#1335. Completes PR libp2p#1313 follow-up: regression test when
new_stream fails, plus towncrier entry.

Co-authored-by: Cursor <cursoragent@cursor.com>
@acul71
Copy link
Copy Markdown
Contributor

acul71 commented May 17, 2026

Hi @TimSC — thank you for opening #1313 and for the clear diagnosis of the ADD_PROVIDER / unbound stream bug. The fix (stream = None plus guarded close in _send_add_provider) was already correct; we pushed a follow-up commit to your provider_store branch so the PR meets the usual libp2p merge requirements.

What was added

  • Tracking issue: #1335 — documents the failure mode when new_stream errors before a stream is opened.
  • Newsfragment: newsfragments/1335.bugfix.rst — user-facing release note for towncrier.
  • Regression test: test_send_add_provider_new_stream_failure in tests/core/kad_dht/test_unit_provider_store.py — mocks new_stream raising and asserts _send_add_provider returns False without a secondary UnboundLocalError.
  • PR description: updated with Fixes #1335 and a completed test plan checklist.
  • Validation: make pr and make linux-docs both passed locally before push.

Branch

Latest commit on TimSC:provider_store: b452e978Add issue, newsfragment, and test for ADD_PROVIDER stream cleanup.

No further changes to your core fix in libp2p/kad_dht/provider_store.py. From here the PR should be ready for maintainer review; happy to adjust anything if you’d like a different wording on the issue or newsfragment.

Thanks again for contributing to py-libp2p.

@acul71 acul71 merged commit 1e113ca into libp2p:main May 17, 2026
38 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.

DHT ADD_PROVIDER fails with unbound stream when new_stream errors

2 participants