Skip to content

file.c: Fix write-format race when opening a stream for playback - #2040

Draft
jlaine wants to merge 1 commit into
asterisk:masterfrom
jlaine:issue-1623
Draft

file.c: Fix write-format race when opening a stream for playback#2040
jlaine wants to merge 1 commit into
asterisk:masterfrom
jlaine:issue-1623

Conversation

@jlaine

@jlaine jlaine commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

openstream_internal() set the channel write format under the channel lock, released the lock, then re-checked that format in filehelper() (ACTION_OPEN) while holding only the formats RWLIST. The bridging and format-negotiation code changes the same channel's write format concurrently (always under the channel lock), so the re-check could observe a different format and wrongly reject a valid sound file with:

Unable to open beep (format (alaw)): No such file or directory

Because the reader never took the channel lock, the set and the re-check were two separate critical sections with the lock dropped in between.

Hold the channel lock across both the write-format set and the filehelper(ACTION_OPEN) re-check so the two accesses share a common lock.

Confirmed with ThreadSanitizer: the data race on the channel write format no longer appears and the intermittent warning no longer reproduces.

Resolves: #1623

@jlaine

jlaine commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

@mbradeen I have not yet had a chance to use this patch in production so handle with care. I'm putting it up here to get some feedback on whether the approach is valid.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Checklist Complete

@github-actions github-actions Bot added the has-pr-checklist A PR Checklist is present on the PR label Jul 26, 2026
@github-actions

Copy link
Copy Markdown

Workflow Check failed
master-ari2-check-709: FAILED TEST: rest_api/channels/create_dial_bridge/create/off_nominal

openstream_internal() set the channel write format under the channel
lock, released the lock, then re-checked that format in filehelper()
(ACTION_OPEN) while holding only the formats RWLIST.  The bridging and
format-negotiation code changes the same channel's write format
concurrently (always under the channel lock), so the re-check could
observe a different format and wrongly reject a valid sound file with:

  Unable to open beep (format (alaw)): No such file or directory

Because the reader never took the channel lock, the set and the re-check
were two separate critical sections with the lock dropped in between.

Hold the channel lock across both the write-format set and the
filehelper(ACTION_OPEN) re-check so the two accesses share a common
lock.

Confirmed with ThreadSanitizer: the data race on the channel write
format no longer appears and the intermittent warning no longer
reproduces.

Resolves: asterisk#1623
@github-actions

Copy link
Copy Markdown

Workflow Check completed successfully

@jlaine

jlaine commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

cherry-pick-to: 20
cherry-pick-to: 22
cherry-pick-to: 23

@github-actions
github-actions Bot dismissed their stale review July 30, 2026 02:17

Pull Request Checklist Complete

@github-actions github-actions Bot removed the has-pr-checklist A PR Checklist is present on the PR label Jul 30, 2026
@jlaine

jlaine commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

I would appreciate any feedback on the potential downsides of holding the channel lock across the "filehelper" call.

@jcolp

jcolp commented Jul 30, 2026

Copy link
Copy Markdown
Member

It should be safe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug]: Bridge dialplan randomly fails to play courtesy tone

2 participants