Skip to content

Commit 0760c29

Browse files
committed
netplay: only reinit preempt frames if core is running
deinit_netplay unconditionally calls preempt_init at the bottom of its body. This is appropriate when netplay is being toggled off mid-session - we want to restore preempt frames so the user gets runahead back. But deinit_netplay is also called from two shutdown contexts where the call is wasted work or worse: - RARCH_CTL_MAIN_DEINIT (retroarch.c:8511) at full app shutdown: preempt frames will be torn down moments later by CMD_EVENT_CORE_DEINIT's preempt_deinit. - retroarch_main_init's error: label (retroarch.c:8431) at init failure: same teardown follows. Beyond the wasted allocation, preempt_init at runahead.c:1501 synchronously calls runloop_st->current_core.retro_run() when frame_count is 0. Firing a synchronous retro_run from inside a deinit chain - while RARCH_CTL_MAIN_DEINIT is on the stack or while we're unwinding from a failed init - is a surprising side effect that any future change to retro_run could turn into a bug. Gate the call on RUNLOOP_FLAG_CORE_RUNNING. The flag is cleared at the top of CMD_EVENT_UNLOAD_CORE (retroarch.c:3749), so the shutdown paths see it unset and skip preempt_init. The mid- session toggle paths (CMD_EVENT_NETPLAY_DEINIT from the netplay menu actions in CMD_EVENT_NETPLAY_ENABLE_HOST etc.) see it set and call preempt_init as before.
1 parent 0ce7158 commit 0760c29

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

network/netplay/netplay_frontend.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9070,8 +9070,22 @@ void deinit_netplay(void)
90709070
| NET_DRIVER_ST_FLAG_NETPLAY_IS_CLIENT);
90719071

90729072
#if HAVE_RUNAHEAD
9073-
/* Reinitialize preemptive frames if enabled */
9074-
preempt_init(runloop_state_get_ptr());
9073+
/* Reinitialize preemptive frames if we're disabling
9074+
* netplay mid-session. Skip if the core isn't running:
9075+
* deinit_netplay is also called from the full shutdown
9076+
* chain (RARCH_CTL_MAIN_DEINIT at retroarch.c:8511) and
9077+
* from the retroarch_main_init error: label, where the
9078+
* subsequent CMD_EVENT_CORE_DEINIT will tear down
9079+
* preempt frames anyway via preempt_deinit. Allocating
9080+
* a fresh preempt frame buffer just to free it seconds
9081+
* later is wasted work, and the synchronous retro_run
9082+
* path inside preempt_init at runahead.c:1501 is a
9083+
* surprising side effect to fire from within a deinit
9084+
* chain. RUNLOOP_FLAG_CORE_RUNNING is cleared at the
9085+
* top of CMD_EVENT_UNLOAD_CORE (retroarch.c:3749),
9086+
* making it the right discriminator. */
9087+
if (runloop_state_get_ptr()->flags & RUNLOOP_FLAG_CORE_RUNNING)
9088+
preempt_init(runloop_state_get_ptr());
90759089
#endif
90769090

90779091
if (net_st->core_netpacket_interface

0 commit comments

Comments
 (0)