Skip to content

[Bug]: HttpStream - race condition in close() and streamType override in final message #549

Description

@chonlaphoom

Bug Description

I've found two bugs in HttpStream (packages/apps/src/http/http-stream.ts):

found in @microsoft/teams.apps version 2.0.5

Bug 1: streamType always remains informative in final message

close() builds the final activity with .addStreamFinal().withChannelData(this.channelData). Since addStreamFinal() sets streamType: 'final' but withChannelData() merges this.channelData afterwards (which contains streamType: 'informative' accumulated from typing updates), the final message's streamType gets overwritten. Teams never receives the correct final stream state.

Reproduction:

stream.update('Thinking...')
stream.emit('Response text')
await stream.close()
// Final message has streamType: 'informative' instead of 'final'

Bug 2: Race condition in close() - final message sent before flush completes

The while loop in close() checks queue.length and this.id but not this._flushing. If flush() has drained the queue but is still awaiting pushStreamChunk() network calls, close() proceeds immediately and sends the final message. This causes the final message to arrive before intermediate stream chunks.

Reproduction:

stream.emit('chunk 1')
stream.emit('chunk 2')
stream.close()
// close() may send final message before chunk 2 finishes sending

Proposed fixes

  1. Bug 1: Swap method order in close() so .withChannelData() runs before .addStreamFinal()
  2. Bug 2: Add || this._flushing to the while condition in close()

I have a working fix with unit tests ready to submit as a PR once approved.

Metadata

Metadata

Assignees

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