stream icon indicating copy to clipboard operation
stream copied to clipboard

From Discord: Bug in Merged Streams Causes Leaking Handle

Open effect-bot opened this issue 1 year ago • 0 comments

Summary

The user is reporting a bug in the @effect/stream library where merging streams is causing a leaking handle. They are unsure if they are misusing the library or if there is a problem with it. They have asked for help from @timsmart and provided a clearer reproduction of the issue. The user has also discovered that swapping the order of the merged streams solves the issue, but only if the first stream is ending. They mention that when using Stream.never, the process hangs forever. Key takeaways from the conversation are that there seems to be a bug in the @effect/stream library related to merging streams and that the order of the merged streams can affect the behavior.

Example article

Bug in Merged Streams Causes Leaking Handle

Issue Description

There seems to be a bug in the @effect/stream library where merging streams can cause a leaking handle. The issue occurs when the first stream in the merge operation does not properly end, resulting in the process hanging indefinitely.

Reproduction Steps

To reproduce the issue, follow these steps:

  1. Create two streams, stream1 and stream2.
  2. Merge the two streams using the merge function from @effect/stream.
  3. Subscribe to the merged stream and observe the behavior.
import { Stream, merge } from '@effect/stream';

const stream1: Stream<number> = ...;
const stream2: Stream<number> = ...;

const mergedStream = merge(stream1, stream2);

mergedStream.subscribe({
  next: (value) => {
    console.log(value);
  },
  complete: () => {
    console.log('Stream completed');
  },
});

Expected Behavior

The merged stream should properly handle the completion of both input streams and emit the expected values.

Actual Behavior

When the first stream does not properly end, such as when using Stream.never, the process hangs indefinitely and the merged stream does not complete.

Workaround

A possible workaround for this issue is to swap the order of the streams in the merge operation. By making sure that the stream that is expected to end first is the first argument to merge, the issue can be avoided.

const mergedStream = merge(stream2, stream1);

Resolution

This bug has been reported to the maintainers of the @effect/stream library. It is recommended to keep an eye on the library's GitHub repository for any updates or fixes related to this issue.

In the meantime, if you encounter this problem, using the workaround of swapping the order of the streams in the merge operation should help mitigate the issue.

Conclusion

The bug in the @effect/stream library where merging streams can cause a leaking handle has been identified. While waiting for a fix from the library maintainers, a workaround of swapping the order of the streams in the merge operation can be used to avoid the issue.

Discord thread

https://discord.com/channels/795981131316985866/1139514795658330184

effect-bot avatar Sep 22 '23 10:09 effect-bot