1515 from types import TracebackType
1616
1717 from apify_client ._resource_clients import LogClient , LogClientAsync
18+ from apify_client .http_clients import HttpResponse
1819 from apify_client .types import Timeout
1920
2021
@@ -98,6 +99,13 @@ class StreamedLog(StreamedLogBase):
9899 call `start` and `stop` manually. Obtain an instance via `RunClient.get_streamed_log`.
99100 """
100101
102+ _stop_timeout_s : ClassVar [float ] = 5
103+ """Upper bound on how long `stop` waits for the streaming thread to finish.
104+
105+ Closing the response only ends the read on a transport that honours it, which neither Impit nor HTTPX does, so
106+ without a bound `stop` would wait for the next chunk, which on a quiet run may be hours away.
107+ """
108+
101109 def __init__ (self , log_client : LogClient , * , to_logger : logging .Logger , from_start : bool = True ) -> None :
102110 """Initialize `StreamedLog`.
103111
@@ -111,14 +119,15 @@ def __init__(self, log_client: LogClient, *, to_logger: logging.Logger, from_sta
111119 super ().__init__ (to_logger = to_logger , from_start = from_start )
112120 self ._log_client = log_client
113121 self ._streaming_thread : Thread | None = None
122+ self ._log_stream : HttpResponse | None = None
114123 self ._stop_logging = False
115124
116125 def start (self ) -> Thread :
117126 """Start the streaming thread.
118127
119128 The caller is responsible for cleanup by calling the `stop` method when done.
120129 """
121- if self ._streaming_thread :
130+ if self ._streaming_thread and self . _streaming_thread . is_alive () :
122131 raise RuntimeError ('Streaming thread already active' )
123132 self ._stop_logging = False
124133 # A daemon thread so a stream still blocked on a read can never hold up interpreter shutdown.
@@ -127,13 +136,29 @@ def start(self) -> Thread:
127136 return self ._streaming_thread
128137
129138 def stop (self ) -> None :
130- """Signal the streaming thread to stop logging and wait for it to finish."""
139+ """Signal the streaming thread to stop logging and wait up to `_stop_timeout_s` for it to finish.
140+
141+ A thread that outlives the wait is a daemon with `_stop_logging` set, so it exits after at most one more chunk,
142+ and only then does its buffered tail reach the logger. Its handle is kept while it is alive, so `start` cannot
143+ revive it beside a second thread on the same buffer.
144+ """
131145 if not self ._streaming_thread :
132146 raise RuntimeError ('Streaming thread is not active' )
133147 self ._stop_logging = True
134- self ._streaming_thread .join ()
135- self ._streaming_thread = None
136- self ._stop_logging = False
148+ # Read once; the streaming thread clears the attribute as soon as the stream ends.
149+ log_stream = self ._log_stream
150+ if log_stream is not None :
151+ try :
152+ log_stream .close ()
153+ except Exception :
154+ # A failing `close` in a custom transport must not fail the caller.
155+ self ._to_logger .exception ('Closing the log stream failed:' )
156+ self ._streaming_thread .join (timeout = self ._stop_timeout_s )
157+ if self ._streaming_thread .is_alive ():
158+ # Otherwise log messages arriving after `stop` returned have no explanation.
159+ self ._to_logger .debug ('Log streaming thread outlived the stop timeout; it ends after the next chunk.' )
160+ else :
161+ self ._streaming_thread = None
137162
138163 def __enter__ (self ) -> Self :
139164 """Start the streaming thread within the context. Exiting the context will finish the streaming thread."""
@@ -151,15 +176,30 @@ def _stream_log(self) -> None:
151176 with self ._log_client .stream (raw = True , timeout = self ._stream_timeout ) as log_stream :
152177 if not log_stream :
153178 return
179+ # Published so `stop` can close the response.
180+ self ._log_stream = log_stream
154181 try :
182+ # `stop` may have run before the response existed for it to close.
183+ if self ._stop_logging :
184+ return
155185 for data in log_stream .iter_bytes ():
156186 self ._process_new_data (data )
157187 if self ._stop_logging :
158188 break
159189 finally :
160- # Flush the last buffered part even if the read timed out or was stopped.
161- self ._log_buffer_content (include_last_part = True )
190+ self ._log_stream = None
191+ try :
192+ # Flush the last buffered part even if the read timed out or was stopped.
193+ self ._log_buffer_content (include_last_part = True )
194+ except Exception :
195+ # A truncated stream leaves an undecodable tail, which is worth a traceback even while a stop
196+ # is in progress.
197+ self ._to_logger .exception ('Log redirection stopped due to unexpected error:' )
162198 except Exception as exc :
199+ if self ._stop_logging :
200+ # `stop` closed the stream out from under the read, so the failure is expected.
201+ self ._to_logger .debug ('Log streaming stopped while `stop` was in progress: %r' , exc )
202+ return
163203 if self ._log_client ._http_client .is_timeout_error (exc ): # noqa: SLF001
164204 # The stream cannot continue, so warn and let the thread end instead of leaking a traceback.
165205 self ._to_logger .warning ('Log streaming stopped: the log stream request timed out.' )
@@ -236,8 +276,13 @@ async def _stream_log(self) -> None:
236276 async for data in log_stream .aiter_bytes ():
237277 self ._process_new_data (data )
238278 finally :
239- # Flush the last buffered part even if the task is cancelled by `stop()`.
240- self ._log_buffer_content (include_last_part = True )
279+ try :
280+ # Flush the last buffered part even if the task is cancelled by `stop()`.
281+ self ._log_buffer_content (include_last_part = True )
282+ except Exception :
283+ # A truncated stream leaves an undecodable tail. Keeping the failure here also keeps the
284+ # cancellation `stop` raised propagating, so the task ends up cancelled as asyncio expects.
285+ self ._to_logger .exception ('Log redirection stopped due to unexpected error:' )
241286 except Exception as exc :
242287 if self ._log_client ._http_client .is_timeout_error (exc ): # noqa: SLF001
243288 # A timeout on the long-lived stream is an expected terminal condition, not an error.
0 commit comments