Skip to content

Fix AsyncBufferSequence leaking base sequence #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

wfltaylor
Copy link

@wfltaylor wfltaylor commented May 14, 2025

Currently, AsyncBufferSequence will leak the base sequence if iteration concludes without next() being cancelled. This is because the internal task retains UnboundedBufferStorage/BoundedBufferStorage causing a reference cycle. This is fixed by adopting an InternalClass inside the iterator which calls an iteratorDeinitialized() function on the storage, as done by other sequences in this package such as combine latest and merge.

The following code illustrates the issue: the assert fails meaning the channel has leaked.

let channel = AsyncChannel<Void>()
            
Task {
    for await _ in channel.buffer(policy: .bounded(1)) {
        break
    }
    
    Task { [weak channel] in
        assert(channel == nil)
    }
}

Task { [weak channel] in
    await channel?.send(())
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants