News for hackers. Stuff that matters.
Re:Fixing stutters in Papers Please on Linux by charcircuit on Sunday January 02, 2022 @07:07AM (#29768075)
From the description of the problem (a freeze every 3 seconds) I knew exactly what it was. You can fix it by simply upgrading SDL as they fixed this bug 2 years ago.

https://github.com/spurious/SDL-mirror/commit/59728f9802c786...

Re:Fixing stutters in Papers Please on Linux by salicideblock on Sunday January 02, 2022 @07:56AM (#29768359)
From one of the printouts in the post, it seems that Papers Please is using a bundled and statically linked SDL.

So it would be the game developer that would have to update the version of the SDL library. The binary patching done seems like a good-enough alternative in the meantime.

I have the feeling such bundling of dependencies is fairly common when porting games for Linux.

Re:Fixing stutters in Papers Please on Linux by edflsafoiewq on Sunday January 02, 2022 @08:12AM (#29768436)
SDL has its own "dynapi" layer, where you can override it with your own copy of SDL even if it was statically linked: https://github.com/libsdl-org/SDL/blob/main/docs/README-dyna...
Re:Fixing stutters in Papers Please on Linux by MayeulC on Sunday January 02, 2022 @11:05AM (#29769831)
Just pray they didn't alter SDL like factorio does: https://news.ycombinator.com/item?id=27246164
Re:Fixing stutters in Papers Please on Linux by ck45 on Sunday January 02, 2022 @08:31AM (#29768555)
Is the version statically linked recent enough to support it? Also, can’t decide if it’s genius or insane, that extra layer of dynamic linking…
Re:Fixing stutters in Papers Please on Linux by charcircuit on Sunday January 02, 2022 @08:35AM (#29768582)
According to [1] it was added (but not released) January 8th, 2014. Papers Please came out on Linux February 12, 2014 so I'd figure it's not in there unless the version of SDL was updated in a later update.

[1] https://old.reddit.com/r/linux_gaming/comments/1upn39/sdl2_a...

Edit: (what I believe to be) This freezing bug was only added 3 years ago, so it might actually have it.

Re:Fixing stutters in Papers Please on Linux by charcircuit on Sunday January 02, 2022 @08:02AM (#29768385)
At least for Steam you are recommended to link against Valve's Steam Linux Runtime which is a set of dynamic libraries including SDL.
Re:Fixing stutters in Papers Please on Linux by viraptor on Sunday January 02, 2022 @08:07AM (#29768406)
It's common in application of certain size and compatibility expectations. Windows and Mac games will bundle their dependencies as much as possible as well. Same for large apps. Nobody wants to end to in a situation where their relatively expensive purchase doesn't work because of the version of local libs.
Re:Fixing stutters in Papers Please on Linux by ironmagma on Sunday January 02, 2022 @10:23AM (#29769393)
Does bundling dependencies imply static linking, though? Why can’t they just dynamically link to the bundled dependency?
Re:Fixing stutters in Papers Please on Linux by viraptor on Sunday January 02, 2022 @07:04PM (#29775588)
Not really, it can be bundled either as static or dynamic.
Re:Fixing stutters in Papers Please on Linux by Diggsey on Sunday January 02, 2022 @07:31AM (#29768203)
Why is it even doing this on the main thread at all? The obvious thing would be to have a background thread polling for changes and then sending messages asynchronously to the main thread if a change actually occurred...
Re:Fixing stutters in Papers Please on Linux by arghwhat on Sunday January 02, 2022 @07:43AM (#29768280)
It is appropriate to use your main thread for your OS interaction - polling fds, talking to display server, whatever I/O you need, etc. An open/close call should never take this long, and you should never need to make a large amount of them in sequence after startup.

What should not be on your main thread is any long blocking compute, which is why rendering/game logic often goes to another thread - although simple games could easily be single-threaded.

Re:Fixing stutters in Papers Please on Linux by lawl on Sunday January 02, 2022 @08:34AM (#29768580)
> An open/close call should never take this long

> What should not be on your main thread is any long blocking compute

Isn't that contradicting yourself? I'm pretty sure open() can block.

Re:Fixing stutters in Papers Please on Linux by arghwhat on Monday January 03, 2022 @04:05AM (#29778768)
> Isn't that contradicting yourself? I'm pretty sure open() can block.

No, blocking compute would be you doing something for a long period of time.

"open/close can block" means very little. You only need a mitigation if it ends up blocking long enough to be a problem in reasonable setups.

You do that have to care about what happens if someone runs the code on an intentionally terrible/horribly slow but technically in spec toy filesystem. No need to prematurely optimize for this scenario.

And especially with devfs you cannot just listen to POSIX and must know what the kernel is providing you - knowing how long operations take on such fds is normal design input.

Re:Fixing stutters in Papers Please on Linux by cturner on Monday January 03, 2022 @07:40AM (#29780178)
"You only need a mitigation if it ends up blocking long enough to be a problem in reasonable setups."

That criteria is established here - the OP is about an issue affecting paying end-users! Premature optimisation is not relevant - the software is failing.

It is unsafe to make fair-weather assumptions about customer systems.

Consider a common software failure: where the user is saving data to a SMB or NFS partition, and then there is a loss of connectivity to that file-server, and the developer has done whatever I/O they need on the main thread. This causes data loss.

You /should/ be able to assume (1) reliably fast return from core async-coordination syscalls (e.g. select, poll), and (2) that you will not suffer process or thread starvation caused by someone else. Respecting those constraints, it is good practice to isolate sync calls to a non-main thread in order to catch when they are not returning. This robustly covers both common scenarios (like the missing-filesystem) and obscure scenarios like the one in blog post.

Re:Fixing stutters in Papers Please on Linux by arghwhat on Monday January 03, 2022 @08:48AM (#29780730)
> That criteria is established here - the OP is about an issue affecting paying end-users! Premature optimisation is not relevant - the software is failing.

The software is not failing, it is experiencing performance degradation: a 500ms pause whenever excessive and entirely unnecessary work is done.

The solution is to not do the work. Moving open/close to a different thread is premature optimization, as no necessary call has been profiled to cause issues on any known system.

Performance 101, do not do things that you do not need done. Even if you want live hotplug and input reconfiguration during gameplay without touching any menus, you only open a device when it appears.

> It is unsafe to make fair-weather assumptions about customer systems.

It is more pointless to optimize for worst-case scenarios - experiencing performance degradation on a faulty system is fine.

All applications have a minimum performance requirement to remain responsive, which is equivalent to always making a certain degree of "fair-weather assumptions".

> Consider a common software failure: where the user is saving data to a SMB or NFS partition, and then there is a loss of connectivity to that file-server, and the developer has done whatever I/O they need on the main thread. This causes data loss.

This is non-sequitur - doing something on the main thread does not cause data-loss. Losing connectivity causes data-loss.

Heck, as main-thread I/O with an event loop implies non-blocking fds, you would not even be blocked by this unless you call fsync(2) to explicitly block until flush is complete, which a normal application does not need to do. The other (horrible) side-effects of network filesystems will cause problems for your application no matter how you interact with the fd.

Furthermore, you cannot use device files without reasoning about their exact implementation. They are not basic files.

> Respecting those constraints, it is good practice to isolate sync calls to a non-main thread in order to catch when they are not returning.

That's a hack, and is not even a solution. What are you going to do when they don't return? Accumulate dead threads and inconsistent shared application state?

Re:Fixing stutters in Papers Please on Linux by cturner on Monday January 03, 2022 @09:30AM (#29781092)
"implies non-blocking fds"

I don't think the open and close calls respect non-blocking on linux when operating on files (specifically files, not sockets - for sockets the return is always quick as far as I know). From man open(2), "I/O operations will (briefly) block when device activity is required, regardless of whether O_NONBLOCK is set". And my recollection is that they will non-briefly block if the problem is a hanging NFS mount.

"What are you going to do when they don't return? Accumulate dead threads and inconsistent shared application state?"

Good point. My practice is to use child processes. These can be killed, and so I do not run into this. But subprocesses ratchets up the amount of work to be done, because you then need to do async IPC. So it's now a lot of extra work. It's even worse for multiplatform stuff because now you are exposed to platform differences (e.g. select is suboptimal on Linux, but poll is not available on Windows).

As you say, using threads within same proc would lead to stale threads. In some contexts this would be tolerable but it is not nearly as simple+clean as I presented.

Thinking hard about addressing this on Linux brings me down, every time. I hope io_uring will make pure async practical within a single process. Even if it does, the multiplatform story will remain complex. I am not fond of your disregard for user data in the (tangential) discussion about disappearing filesystems, but you have won me over to embracing the main thread in this context.

Re:Fixing stutters in Papers Please on Linux by dagmx on Sunday January 02, 2022 @02:11PM (#29772419)
You shouldn't be doing IO on your game thread though. (Main and game thread may differ)
Re:Fixing stutters in Papers Please on Linux by ninepoints on Sunday January 02, 2022 @07:35AM (#29768225)
Because architectural simplicity is valuable and some operating systems deliver input events on a particular thread.
Re:Fixing stutters in Papers Please on Linux by flohofwoe on Sunday January 02, 2022 @09:07AM (#29768778)
The problem probably only shows up on some machines and hasn't been noticed during development and testing. And TBH, polling what input devices are connected should never take more than a few microseconds, no matter how much operating system code sits between the hardware and game code.
Re:Fixing stutters in Papers Please on Linux by charcircuit on Sunday January 02, 2022 @07:45AM (#29768292)
The simple answer is that it wasn't needed for udev. It may not have blown up on the dev's machine because their input devices were different. It might be tested less than the udev version. As the other commenter stated it's simpler to just check every 3 seconds instead of adding threading.
Re:Fixing stutters in Papers Please on Linux by iqanq on Sunday January 02, 2022 @07:20AM (#29768144)
This is one of the reasons I use Gentoo in my desktops: you can run a "stable" (as in old) system, but pull a more recent version of a library or application if you need it. For example, I remember having problems accessing files that I had stored in my mobile phone. Solution: updating libmtp and libmtp only. I suppose you can't do this in a distro such as Debian without upgrading half the packages.

Are there more distros that allow you to do this?

Re:Fixing stutters in Papers Please on Linux by teddyh on Sunday January 02, 2022 @07:48AM (#29768308)
If you are advanced enough to run Gentoo, you should be able to use Debian and force an install of (or re-compile yourself) a new package of the newer version, working around the fact that the official newer version would otherwise require other new packages.
Re:Fixing stutters in Papers Please on Linux by vlovich123 on Sunday January 02, 2022 @10:37AM (#29769522)
You could. But the amount of time it takes is significantly more than yay -S <package> or emerge <package> vs the hell that this poses on Debian (not to mention the dependency hell you can run into)
Re:Fixing stutters in Papers Please on Linux by yjftsjthsd-h on Sunday January 02, 2022 @11:38AM (#29770194)
I don't know the situation on Gentoo, but partial updates are explicitly unsupported on Arch, not least because they don't do stable ABIs; Debian should have a much easier time upgrading just one package.
Re:Fixing stutters in Papers Please on Linux by elevader on Sunday January 02, 2022 @12:59PM (#29771439)
It's not necessarily recommended to mix stable and testing but it mostly works fine in my experience. I'd guess Gentoo gets around quite a few problems as everything is compiled from source. So updating a single libary would cause a rebuild of everything that depends on it.

Gentoo also has the concept of "Slots", so you could have multiple versions of the same libary installed and packages will choose their version to build against accordingly.

Re:Fixing stutters in Papers Please on Linux by vlovich123 on Sunday January 02, 2022 @12:18PM (#29770750)
Having used Arch and Debian, I‘ve definitely had an easier time installing the latest version of arbitrary packages on Arch. Something like SDL is part of base and thus is already running latest.
Re:Fixing stutters in Papers Please on Linux by AshamedCaptain on Sunday January 02, 2022 @09:23AM (#29768881)
Why is udev not used in this case?
Re:Fixing stutters in Papers Please on Linux by bdowling on Sunday January 02, 2022 @07:57PM (#29776041)
Probably because a commercial developer is not going to want to link a LGPL library with their program. [0]

https://github.com/systemd/systemd/blob/main/src/libudev/lib...

Re:Fixing stutters in Papers Please on Linux by charcircuit on Sunday January 02, 2022 @10:29AM (#29769444)
Because this is the fallback when you compile without udev support
Re:Fixing stutters in Papers Please on Linux by AshamedCaptain on Sunday January 02, 2022 @10:42AM (#29769570)
I can guess that, but I was wondering why ... Is there a distro without udev? The steam/whatever sandbox does not support udev?
Re:Fixing stutters in Papers Please on Linux by Sjonny on Sunday January 02, 2022 @11:13AM (#29769928)
It's not the system that is not supporting udev, it's the choice of the game developers how they compiled SDL .. without dependencies, and so without udev.
Re:Fixing stutters in Papers Please on Linux by mschuster91 on Sunday January 02, 2022 @02:58PM (#29773051)
That's standard for game devs in the Linux world. The less dependencies you have to rely on the distribution for, the better - Windows stuff is either already present or shared OS-wide with binary backwards compatibility (=DirectX), so you can get away with shipping stuff that has a chance to run even 25 years in the future without major modification.