AvPlayer: deliver player events from a per-player event thread (A Plague Tale: Innocence) #2480
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!2480
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2480"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Nawid3333 on GitHub · 2026-11-21 01:08 UTC · original
Nawid3333:fix/avplayer-ready-event→main· +236 −51 · 2 files · 4 commits · openA Plague Tale: Innocence (#1994) crashes right after it adds its logo video (@Crispy81, official build
a1fc5ae):sceAvPlayerAddSourcecalls the game's event callback with READY (event 2) before it returns. The fault context of the earlierbc934efrun (#1994 comment) shows the game's READY handler writing the video size (1920x1080, and half of it into a second object) through an object pointer that is still null, so the game sets that object up aftersceAvPlayerAddSourcereturns. shadPS4 and AnyPS5 deliver player events from a player thread as well, so the game's code after the call runs first.Change
AvPlayerEvent) that delivers its events in order.sceAvPlayerAddSource,Start,Stop,Pause,Resume,JumpToTimeand the source's warnings queue their events instead of calling the game directly. With auto-start,sceAvPlayerAddSourcestill starts playback before it returns and queues READY and PLAY together.sceAvPlayerClosestops the thread before it destroys the source: events that are still queued are dropped, and no callback runs afterClosereturns. AClosefrom inside the player's own callback detaches the thread instead of joining it.sceAvPlayerInitfails when the event thread cannot be created.Close, decided under the queue's lock, so aStopon another thread does not race aClose.Test
avplayer_file_tests,CheckStateEvents: a recording callback whose READY handler waits untilsceAvPlayerAddSourcehas returned. Cases: manual start (READY), auto-start (READY, PLAY),Closefrom the READY handler (READY only), auto-start thenStop(READY, PLAY, STOP), andInitfailing when the event thread cannot be created. Without the change the first case fails with "AddSource returns while the READY handler runs on the player's event thread" (measured on maina1fc5ae;avPlayer.cppis the same ona21fd80c8). With the change all cases pass (5 of 5 runs, Windows 11, clang-cl); CI runsavplayer_file_testson Windows, Linux and macOS.Game
@Crispy81 ran a fork build with the event thread (run 44820092766, d9bdbb0 = main
a1fc5ae+ the first commit of this PR): "On that fork, game works fine. Good framerate, some minor graphical issues. No crashes." (#1994 comment). The second commit (the queue's lock and the STOP case) came after that run.Written with Claude Code (AI): Claude Code found the cause from the tester's logs, wrote the change and the tests, and ran them.
@coderabbitai[bot] on GitHub · 2026-11-21 01:14 UTC · original · edited 2026-11-24 07:42 UTC
Walkthrough
AvPlayer now dispatches callback events through an EventQueue worker. The worker starts when a player has an event callback, and player creation fails if the worker cannot start. Source and player operations post state and warning events to the queue. Tests check event ordering, callback thread identity, close behavior, and player creation when thread creation fails.
Suggested reviewers:
nmzik,cosmoChange: Bug fix
Merge Risk:
Moderate · up to
cb7c1Events now run on a separate thread, but nothing makes the READY callback wait until sceAvPlayerAddSource has returned. The game's READY handler can therefore still run too early and hit the null-object access this change targets. This is a timing-dependent race that should be fixed before merge, or the risk explicitly accepted.
Pre-merge checks:
4
Full details: Title check
Explanation
The title names the main change: AvPlayer events are now delivered from a per-player event thread. It also names the game that the change fixes. The raw summary confirms that avPlayer.cpp adds an EventQueue worker thread for player events, so the title matches the changeset.
Full details: Description check
Explanation
The description explains the crash in A Plague Tale: Innocence, the cause (the READY callback runs before sceAvPlayerAddSource returns), the per-player event thread change, the Close and Init behavior, the test cases, and the game validation. It matches the raw summary of avPlayer.cpp and the test file changes.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Share
@coderabbitai[bot] on GitHub · 2026-11-21 01:15 UTC · original · reviewed
190cc2ae-0538-44f7-b30c-8f5ec719fcb0Reviewing files that changed from the base of the PR and between
a21fd80c8eand5d09e324b9.src/libs/avPlayer.cpptests/AvPlayerFileTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
@coderabbitai[bot] on GitHub · 2026-11-24 07:16 UTC · original · reviewed
f6dc624c-711f-467b-9149-20642ade6209Reviewing files that changed from the base of the PR and between
5d09e324b9and2baacf50df.src/libs/avPlayer.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
@Nawid3333 on GitHub · 2026-11-24 07:40 UTC · original
@coderabbitai Both points are right.
2baacf50d:Stopposts STOP inside the lifecycle lock.AvPlayerClosefrees the queue only afterh->source.reset(), whoseClosecallsStopand waits for that lock, so the post finishes first.cb7c1bf61:Start,Pause,Resumeand theAddSourceauto-start also queue READY, PLAY and PAUSE while they hold that lock, so aStopon another thread queues STOP before or after the whole READY/PLAY batch.Posttakes only the queue's own mutex, and the event thread calls the game with neither lock held, so this adds no lock-order dependency.@coderabbitai[bot] on GitHub · 2026-11-24 07:42 UTC · original · reviewed
Actionable comments posted: 1
c289dd01-3b1b-4e25-93b6-ad10f20cf815Reviewing files that changed from the base of the PR and between
2baacf50dfandcb7c1bf61b.src/libs/avPlayer.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.
@ -800,0 +895,4 @@const std::array<EventQueue::Event, 2> added {{{AVPLAYER_EVENT_STATE_READY, std::nullopt},{AVPLAYER_EVENT_STATE_PLAY, std::nullopt}}};events->Post(std::span(added).first(start && rc == 0 ? 2 : 1));@coderabbitai[bot] on GitHub · 2026-11-24 07:42 UTC · original · edited 2026-11-24 11:31 UTC
Synchronize READY delivery with caller setup.
After
Postreleases the queue mutex, the worker can invoke READY beforeSource::AddorsceAvPlayerAddSourcereturns. The lifecycle lock does not prevent callback execution. The reported null-object access therefore remains possible.Use an explicit readiness handshake tied to completion of caller setup. Moving the post or adding a delay cannot guarantee this ordering. The current test does not establish that guarantee because
RecordEventwaits inside the READY handler foradd_source_returned. (raw.githubusercontent.com)@coderabbitai[bot] on GitHub · 2026-11-24 07:43 UTC · original
Analysis chain
Script executed:
Length of output: 16723
Script executed:
Length of output: 15853
@Nawid3333Thanks. I checked commitcb7c1bf61.The lifecycle lock serializes
Stopwith the READY/PLAY batch and the other state-event posts. The event worker releases the queue mutex before invoking the callback.If
Stopalready holds the lifecycle lock,Closewaits for its post before freeing the queue. Shutdown can still discard that post. This does not establish safety for a call racing withClosebefore it acquires the lifecycle lock; that would require separate handle and source lifetime coordination if concurrent close is supported.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
You are interacting with an AI system.
@ -800,0 +895,4 @@const std::array<EventQueue::Event, 2> added {{{AVPLAYER_EVENT_STATE_READY, std::nullopt},{AVPLAYER_EVENT_STATE_PLAY, std::nullopt}}};events->Post(std::span(added).first(start && rc == 0 ? 2 : 1));@Nawid3333 on GitHub · 2026-11-24 11:31 UTC · original
The window left is between the event thread and the game's own code after
sceAvPlayerAddSourcereturns. No AvPlayer function marks the end of that setup (libAudio.cpp:667-694), so a handshake could wait at most for the return itself, and the game's next instructions would still race the event thread.AvPlayerInitDataExhas settings for aneventthread of the player's own (avPlayer.cpp:254).What the READY handler can read through the player is set before the post:
Init(1870),h->source(1874), and with auto-startAdd(1875) runsStartImplbeforePost(893-898). The queue mutex orders this for the event thread (174, 197-206).The test checks the change itself: READY no longer runs inside
AddSourceon the caller's thread; with main'savPlayer.cppit fails. It does not claim an order after the return.@ -800,0 +895,4 @@const std::array<EventQueue::Event, 2> added {{{AVPLAYER_EVENT_STATE_READY, std::nullopt},{AVPLAYER_EVENT_STATE_PLAY, std::nullopt}}};events->Post(std::span(added).first(start && rc == 0 ? 2 : 1));@coderabbitai[bot] on GitHub · 2026-11-24 11:32 UTC · original
Analysis chain
Script executed:
Length of output: 15650
@Nawid3333You’re right. I withdraw my recommendation for a caller-setup handshake: AvPlayer has no signal for when the game finishes that setup.The source initialization and
h->sourceassignment happen before READY is posted. Successful auto-start also happens before the post. The queue mutex makes those writes visible to the callback.READY can still run before
sceAvPlayerAddSourcereturns or before the game finishes its setup. The test checks separate-thread delivery, not that ordering. I’m not marking the timing concern as addressed.The reported game run used the first PR commit. A run on the final revision would provide additional validation, though it would not prove an ordering guarantee.
You are interacting with an AI system.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.