Skip to content

Fix bare except in Stream_accept that swallows all exceptions#1632

Open
Andy-Jost wants to merge 2 commits intoNVIDIA:mainfrom
Andy-Jost:fix-stream-accept-bare-except
Open

Fix bare except in Stream_accept that swallows all exceptions#1632
Andy-Jost wants to merge 2 commits intoNVIDIA:mainfrom
Andy-Jost:fix-stream-accept-bare-except

Conversation

@Andy-Jost
Copy link
Contributor

Summary

  • Narrows bare except: pass in Stream_accept to except TypeError with a string check for "__cuda_stream__", so only the "protocol not supported" case is silently handled
  • Buggy __cuda_stream__ implementations, RuntimeError, KeyboardInterrupt, SystemExit, etc. now propagate correctly

Closes #1631

Changes

  • _stream.pyx: Replace except: pass with except TypeError as e: if "__cuda_stream__" not in str(e): raise

Test Coverage

  • Existing Stream_accept and stream protocol tests cover the happy path
  • The change is a strict narrowing of the catch clause; no new behavior is introduced for well-formed objects

Made with Cursor

@Andy-Jost Andy-Jost added this to the cuda.core v0.7.0 milestone Feb 17, 2026
@Andy-Jost Andy-Jost added bug Something isn't working P1 Medium priority - Should do cuda.core Everything related to the cuda.core module labels Feb 17, 2026
@Andy-Jost Andy-Jost self-assigned this Feb 17, 2026
@copy-pr-bot
Copy link
Contributor

copy-pr-bot bot commented Feb 17, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@Andy-Jost
Copy link
Contributor Author

/ok to test a1614ed

@Andy-Jost Andy-Jost requested a review from cpcloud February 17, 2026 21:57
@Andy-Jost
Copy link
Contributor Author

I found this issue as a follow-on to this comment while searching for other places where try...except was used to swallow exceptions too broadly.

Comment on lines 512 to 514
except TypeError as e:
if "__cuda_stream__" not in str(e):
raise
Copy link
Contributor Author

Choose a reason for hiding this comment

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

cf.

try:
cuda_stream_attr = obj.__cuda_stream__
except AttributeError:
raise TypeError(f"{type(obj)} object does not have a '__cuda_stream__' attribute") from None

Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we swallowing an AttributeError, just to tell the user "The thing doesn't have this attribute"? This seems super roundabout, and adding a string contains check to this oddity doesn't seem like the right approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the overall result is correct. If someone passes an arbitrary object to the stream protocol and that object lacks a __cuda_stream__ attribute, then raising TypeError is the correct response. This matches standard protocols such as iter(), len(), and hash().

That said, I think you're right that it would be arguably cleaner to use hasattr at line 474 rather than catch the AttributeError, though that would mean checking hasattr and then calling the equivalent of getattr separately.

The problem is that Python does not provide a clean method of detecting when an object lacks a specific protocol, and adding something custom here, like ProtocolNotSupportedError, would be even more out of the norm.

I don't see a better option than catching the TypeError and narrowing it with this heuristic to make sure it looks like the one we expect.

Copy link
Contributor Author

@Andy-Jost Andy-Jost Feb 19, 2026

Choose a reason for hiding this comment

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

Actually, now that I say that, perhaps Stream_accept near line 512 could just use hasattr to check for __cuda_stream__. But that raises its own complications, since Stream._init has many paths besides the stream protocol one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the latest upload is probably what you had in mind.

@github-actions
Copy link

@Andy-Jost Andy-Jost requested a review from leofang February 17, 2026 22:24
@leofang
Copy link
Member

leofang commented Feb 18, 2026

@Andy-Jost Just to confirm, this PR can wait after we release v0.6.0, or do you want this to be part of v0.6.0 as well?

@Andy-Jost
Copy link
Contributor Author

This is not a critical fix, so I put it under v0.7.0. But I think it is very safe, so if it gets approved in time I will include it with v0.6.0.

Copy link
Contributor

@cpcloud cpcloud left a comment

Choose a reason for hiding this comment

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

Can we fix this without piling on another hack?

Comment on lines 512 to 514
except TypeError as e:
if "__cuda_stream__" not in str(e):
raise
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we swallowing an AttributeError, just to tell the user "The thing doesn't have this attribute"? This seems super roundabout, and adding a string contains check to this oddity doesn't seem like the right approach.

Andy-Jost and others added 2 commits February 19, 2026 09:41
…#1631)

Narrow the catch from bare `except: pass` to `except TypeError`
with a string check, so that only missing `__cuda_stream__` is
silently handled. Buggy protocol implementations and other
exceptions now propagate correctly.

Co-authored-by: Cursor <cursoragent@cursor.com>
@Andy-Jost Andy-Jost force-pushed the fix-stream-accept-bare-except branch from a1614ed to 2f81e58 Compare February 19, 2026 17:42
@Andy-Jost
Copy link
Contributor Author

/ok to test 2f81e58

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working cuda.core Everything related to the cuda.core module P1 Medium priority - Should do

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stream_accept uses bare except that swallows all exceptions

3 participants

Comments