@@ -166,7 +166,7 @@ cdef class Event:
166166 raise RuntimeError (explanation)
167167
168168 def __hash__ (self ) -> int:
169- """Return hash based on the underlying CUevent handle address.
169+ """Return hash based on the underlying CUevent handle address and context .
170170
171171 This enables Event objects to be used as dictionary keys and in sets.
172172 Two Event objects wrapping the same underlying CUDA event will hash
@@ -175,15 +175,23 @@ cdef class Event:
175175 Returns
176176 -------
177177 int
178- Hash value based on the event handle address.
178+ Hash value based on the event handle address and context handle.
179+
180+ Notes
181+ -----
182+ Includes the context handle in the hash to prevent collisions when
183+ handles are reused across different contexts. While handles are
184+ context-scoped and typically not reused across contexts , including
185+ the context provides defense-in-depth against hash collisions.
179186
180187 Warning
181188 -------
182189 Using a closed or destroyed event as a dictionary key or in a set
183190 results in undefined behavior. The event handle may be reused by
184191 the CUDA driver for new events.
185192 """
186- return hash((type(self ), <uintptr_t>(self._handle )))
193+ # Context should always be set as a post-condition of Event construction
194+ return hash((type(self ), <uintptr_t>(int(self._ctx_handle )), <uintptr_t>(self._handle )))
187195
188196 def __eq__(self , other ) -> bool:
189197 """Check equality based on the underlying CUevent handle address.
0 commit comments