Skip to content

Conversation

pzerelles
Copy link

Maybe fixes #462 and #472.

Without this, I cannot start an agent using Bun runtime.

Copy link

changeset-bot bot commented Oct 1, 2025

⚠️ No Changeset found

Latest commit: 48a58cb

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@CLAassistant
Copy link

CLAassistant commented Oct 1, 2025

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@Shubhrakanti Shubhrakanti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please write what the root cause is, why this is only an issue only with bun and why setImmediate solved it?

} else if (ev.case == 'localTrackPublished') {
const publication = this.localParticipant!.trackPublications.get(ev.value.trackSid!);
this.emit(RoomEvent.LocalTrackPublished, publication!, this.localParticipant!);
setImmediate(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add a comment on why we use setImmediate here.

Copy link
Author

@pzerelles pzerelles Oct 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, the root cause is, that the localTrackPublished event is processed before this.localParticipant.trackPublications has the track in the map.

This happens, because publishTrack in participant.ts sends the publishTrack request to the FfiClient. Then it waits for the publishTrack event from the FfiClient with the result. The result is used to set the track in the trackPublications map.

Right after the publishTrackevent is emitted by the FfiClient, the localTrackPublished event is also emitted. Since the publishTrack function in participant.ts is async and awaits the arrival of the publishTrack event, it is not guaranteed that this code will resume before the event loop has finished with other things, like calling the localTrackPublished handler.

By using setImmediate() in the localTrackPublished handler, we ensure that the event loop has processed the awaited promises before we try to access the trackPublications map.

Since I don't see the whole picture, this might be solved by not relying on execution order in an asynchronous scenario like it is the case, and refactoring might be a better solution. But this is maybe a bigger thing and using setImmediate is a quick fix for the current version.

This is a problem with Bun currently, because it seems to process the event loop differently. But since there is no guarantee, this could break in a future version of NodeJS, too.

Copy link
Contributor

@lukasIO lukasIO Oct 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using setImmediate is a quick fix for the current version

this is the only thing I'm a bit concerned about. It doesn't really address the underlying issue that what we'd want to be doing is consecutively process each incoming event so that execution order is guaranteed.

By setImmediateing one of them, we just potentially kick the can down the road

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a general problem the way events are handled with waitFor. The event handler should directly perform what is needed, not just resolve a promise that causes another function to continue, because the execution order is not guaranteed for that. One way to handle this maybe is to change waitFor in publishTrack to an event handler that adds the track to trackPublications.

@tobowers
Copy link

tobowers commented Oct 7, 2025

would love to get this in, even in a partial fix state because we run into this error all the time and would be amazing to get it cleaned up while working out how to properly do events one after the other.

@lukasIO
Copy link
Contributor

lukasIO commented Oct 8, 2025

I opened #547 to address this more generally, could you try with these changes and report if it works as expected for you?

@tobowers
Copy link

I opened #547 to address this more generally, could you try with these changes and report if it works as expected for you?

if this was directed at me then I can't really test that because it happens in production usually and not locally.

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.

Race condition for local track subscribed

5 participants