Skip to content

Fix crash from playing too many demos#363

Open
betsruner wants to merge 2 commits into
p2sr:masterfrom
betsruner:fix-demo-crash
Open

Fix crash from playing too many demos#363
betsruner wants to merge 2 commits into
p2sr:masterfrom
betsruner:fix-demo-crash

Conversation

@betsruner
Copy link
Copy Markdown
Member

This fixes the longstanding crash from playing back too many demos.

The basic issue was that when demos were played back it loaded in the models with a protected flag that prevented some of them from being deleted when the demo stopped. This meant that every time a new map was loaded it would slowly increase until the cache (256MB) didn't have enough space causing a crash :(. Repeatedly playing the demos of the same map doesn't cause the crash it's gotta be unique maps/model data.

I tried a few things to fix it, like forcibly unloading the models, that just crashes the game. Using built-in Valve commands (flush_locked explicitly flushes these resources) on demo stop, also just crashes the game. Just upping the cache limit to 512MB, which actually worked and made the cache sufficiently sized such that you could playback the entire SP or Coop campaign without crashing. It just wasn't a fix for the core issue so I abandoned it.

The solution I ended up on is whenever a demo stops it looks through the models cache and changes the flags to be ones the game is willing to cleanup. From there the game cleans them up appropriately by itself which nicely fixes the issue :).

Variable is sv_cheats protected because it has no need for working in a run, and it'd be pointless to waste time testing whether or not it can somehow actually break something.

Tested Portal 2 + all the mods on windows + linux (if Linux is supported). Pretty sure Valve just never changed anything related to this across all the engine versions so it kind of just worked out nicely.

@betsruner
Copy link
Copy Markdown
Member Author

betsruner commented May 27, 2026

Oh yeah if you wanted to test this yourself you can use the command cache_print_summary at the end of every demo.
Section [ModelData]: 0 resources total 0 bytes, 0.00 % of limit (256 MB)
Section [ModelMesh]: 0 resources total 0 bytes, 0.00 % of limit (256 MB)
Section [AnimBlock]: 0 resources total 0 bytes, 0.00 % of limit (256 MB)
Section [ColorMesh]: 0 resources total 0 bytes, 0.00 % of limit (256 MB)
Section [Captions]: 0 resources total 0 bytes, 0.00 % of limit (64 KB)

As you play more unique maps in demos (without the fix active) you'll find ModelMesh grows rather rapidly!

@ThisAMJ
Copy link
Copy Markdown
Member

ThisAMJ commented May 27, 2026

Love it! I'll just get you to regenerate docs/cvars.md whenever you add/remove/etc cvars (see #p2sr-dev pins), and fix a slight code smell by noting // namespace on the closing brace, which is preferred on long blocks / ifdefs where the initiator may be unclear.

Have you tested this on other mods too? NBD if not, it will correctly disable if the sig fails...

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