Skip to content

return in a generator function crashes after iterating an @azure/core-sse EventMessageStream to the end #30414

Closed
@jaked

Description

  • Package Name: @azure/core-sse
  • Package Version: 2.1.2
  • Operating system: MacOS
  • nodejs
    • version: 20.10.0
  • browser
    • name/version:
  • typescript
    • version:
  • Is the bug related to documentation in

Describe the bug
Returning from an iteration over an EventMessageStream crashes if the entire stream has already been consumed.

To Reproduce
Steps to reproduce the behavior:

  1. obtain a stream with createSseStream (e.g. from an @azure-rest/ai-inference response body)
  2. iterate the stream in a generator function (with for await (const event of stream))
  3. return from the iterator when complete (e.g. with if (event.data === "[DONE]") return)
  4. observe exception:
TypeError: Cannot read properties of null (reading 'end')
    at Object.cancel (file:///Users/jaked/repos/githubnext/vitale/node_modules/.pnpm/@azure+core-sse@2.1.2/node_modules/@azure/core-sse/dist/esm/utils.js:54:31)
    at readableStreamDefaultControllerCancelSteps (node:internal/webstreams/readablestream:2383:39)
    at ReadableStreamDefaultController.[kCancel] (node:internal/webstreams/readablestream:1079:12)
    at ensureIsPromise (node:internal/webstreams/util:185:19)
    at readableStreamCancel (node:internal/webstreams/readablestream:1984:5)
    at readableStreamReaderGenericCancel (node:internal/webstreams/readablestream:2124:10)
    at returnSteps (node:internal/webstreams/readablestream:524:24)
    at Object.return (node:internal/webstreams/readablestream:568:11)
    at main (/Users/jaked/repos/githubnext/vitale/packages/examples/azure-ai.vnb-cellId=fUdPs5uLJxWHELOq-T1-8.tsx:40:3)
    at main.next (<anonymous>)

Expected behavior
Iteration completes normally without exception.

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
The exception is coming from the cancel function in @azure/core-sse/utils.js, which calls stream.socket.end() when an EventMessageStream iteration returns in a generator function (since #28111). But Node sets stream.socket to null when the underlying message has been consumed (I think since nodejs/node#38505 / nodejs/undici#834), so the call fails.

Replacing stream.socket.end() with stream.socket?.end() fixes the issue for me.

Metadata

Assignees

Labels

Azure.CoreClientThis issue points to a problem in the data-plane of the library.Service AttentionWorkflow: This issue is responsible by Azure service team.bugThis issue requires a change to an existing behavior in the product in order to be resolved.customer-reportedIssues that are reported by GitHub users external to the Azure organization.issue-addressedWorkflow: The Azure SDK team believes it to be addressed and ready to close.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions