|
12 | 12 | from datetime import datetime, timedelta |
13 | 13 | from threading import Event, Thread |
14 | 14 | from types import GeneratorType |
15 | | -from typing import Any, Generator, Optional, Sequence, TypeVar, Union |
| 15 | +from typing import Any, Generator, Iterator, Optional, Sequence, TypeVar, Union |
16 | 16 |
|
17 | 17 | import grpc |
18 | 18 | from google.protobuf import empty_pb2 |
|
38 | 38 | otel_tracer = None |
39 | 39 |
|
40 | 40 |
|
41 | | - |
42 | 41 | class VersionNotRegisteredException(Exception): |
43 | 42 | pass |
44 | 43 |
|
@@ -283,7 +282,7 @@ class TaskHubGrpcWorker: |
283 | 282 | activity function. |
284 | 283 | """ |
285 | 284 |
|
286 | | - _response_stream: Optional[grpc.Future] = None |
| 285 | + _response_stream: Optional[Union[Iterator[grpc.Future], grpc.Future]] = None |
287 | 286 | _interceptors: Optional[list[shared.ClientInterceptor]] = None |
288 | 287 |
|
289 | 288 | def __init__( |
@@ -421,9 +420,12 @@ def invalidate_connection(): |
421 | 420 | # Cancel the response stream first to signal the reader thread to stop |
422 | 421 | if self._response_stream is not None: |
423 | 422 | try: |
424 | | - self._response_stream.cancel() |
425 | | - except Exception: |
426 | | - pass |
| 423 | + if hasattr(self._response_stream, "call"): |
| 424 | + self._response_stream.call.cancel() # type: ignore |
| 425 | + else: |
| 426 | + self._response_stream.cancel() # type: ignore |
| 427 | + except Exception as e: |
| 428 | + self._logger.warning(f"Error cancelling response stream: {e}") |
427 | 429 | self._response_stream = None |
428 | 430 |
|
429 | 431 | # Wait for the reader thread to finish |
@@ -740,7 +742,13 @@ def stop(self): |
740 | 742 |
|
741 | 743 | self._logger.info("Stopping gRPC worker...") |
742 | 744 | if self._response_stream is not None: |
743 | | - self._response_stream.cancel() |
| 745 | + try: |
| 746 | + if hasattr(self._response_stream, "call"): |
| 747 | + self._response_stream.call.cancel() # type: ignore |
| 748 | + else: |
| 749 | + self._response_stream.cancel() # type: ignore |
| 750 | + except Exception as e: |
| 751 | + self._logger.warning(f"Error cancelling response stream: {e}") |
744 | 752 | self._shutdown.set() |
745 | 753 | # Explicitly close the gRPC channel to ensure OTel interceptors and other resources are cleaned up |
746 | 754 | if self._current_channel is not None: |
@@ -854,13 +862,15 @@ def _execute_activity( |
854 | 862 |
|
855 | 863 | if otel_tracer is not None: |
856 | 864 | span_context = otel_tracer.start_as_current_span( |
857 | | - name=f'activity: {req.name}', |
858 | | - context=otel_propagator.extract(carrier={"traceparent": req.parentTraceContext.traceParent}), |
| 865 | + name=f"activity: {req.name}", |
| 866 | + context=otel_propagator.extract( |
| 867 | + carrier={"traceparent": req.parentTraceContext.traceParent} |
| 868 | + ), |
859 | 869 | attributes={ |
860 | 870 | "durabletask.task.instance_id": instance_id, |
861 | 871 | "durabletask.task.id": req.taskId, |
862 | 872 | "durabletask.activity.name": req.name, |
863 | | - } |
| 873 | + }, |
864 | 874 | ) |
865 | 875 | else: |
866 | 876 | span_context = contextlib.nullcontext() |
|
0 commit comments