Skip to content

[Bug]: HttpStream doesn't handle canceling of stream #332

@jerbob92

Description

@jerbob92

Bug Description

It would be nice if this could be supported, I have implemented it myself now in a wrapper for HttpStream. If anyone is interested in it:

class CustomHttpStream(HttpStream):
    def __init__(self, client: ApiClient, ref: ConversationReference):
        super().__init__(client, ref)
        self._is_canceled = False

    def emit(self, activity: Union[MessageActivityInput, TypingActivityInput, str]) -> None:
        if self.is_canceled():
            logger.warning("Not emitting activity, stream is canceled.")
            return None
        return super().emit(activity)

    async def _send(self, to_send: Union[TypingActivityInput, MessageActivityInput]) -> SentActivity:
        try:
            res = await super()._send(to_send)
            return res
        except HTTPStatusError as e:
            if e.response.status_code == 403:
                # Set canceled and return a `CancelledError` so that the
                # request won't be retried.
                self._is_canceled = True
                logger.warning("Teams channel stopped the stream.")
                raise asyncio.CancelledError("Teams channel stopped the stream.")
            raise e
        except Exception as e:
            raise e

    def is_canceled(self) -> bool:
        return self._is_canceled

Steps to Reproduce

  1. Start a stream
  2. Send messages
  3. Cancel from Teams
  4. HttpStream tries to continue sending updates in a retry, no way to see that it was canceled

Expected Behavior

HttpStream should stop attempt sending updates, and it should be possible to see that it was canceled.

Actual Behavior

HttpStream tries to continue sending updates in a retry, there is no way to see that it was canceled

SDK Version

20.0.0a20

Python Version

3.14.2

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions