Periodic input stall: device enumeration blocks the input thread every ~5.4 seconds

NewoNZ shared this bug 20 days ago
Submitted

TITLE: Periodic camera/input freeze (~200 ms every ~5.4 s): input-device enumeration on a VRageWorker blocks the input thread


GAME: Space Engineers 2, app 4.0.0.3169, offline single-player

OS: Windows 11 build 26200 · CPU: Intel 12th-gen i7 · GPU: NVIDIA RTX 50-series, driver 621.14 · 1440p/144 Hz, G-Sync


SYMPTOM

While moving, the view freezes for a split second and then jumps ahead, on a steady rhythm (about 5.4 s in this capture, 2.5–5.4 s in other sessions). FPS counters and frame-time tools show nothing, because frames are presented on time with identical content.


ROOT CAUSE (measured with Windows Performance Recorder, 36 s capture)

1. About every 5.37 s, a VRageWorker thread (0/1/2/6/9) runs a job that enumerates input/HID devices and resolves their names. Each run opens about 750 Windows driver INF files (C:\Windows\INF\input.inf/.PNF, DriverStore\en-US\input.inf_loc, hidserv.inf/.PNF/_loc, keyboard.inf_loc, oem49.inf = Intel HID Event Filter) and costs about 130 ms of CPU. Defender scans each open.

2. The game's input thread blocks on that job. It's an unnamed thread that normally wakes about 4,200 times/s in win32k and the USB stack. It leaves the CPU with wait reason WrAlertByThreadId for 196–200 ms.

3. In 7 of 7 stalls, the readying thread that releases the input thread is the worker that just finished the enumeration.

4. While it's blocked, rendering (about 125 fps) and the server tick continue, but no input is applied: about 200 ms of identical view, then a catch-up jump.


PER-STALL DATA (input thread blocked / duration / waker / INF opens / frames presented in window / longest frame gap)

23:01:05.544 199.1 ms VRageWorker 1 746 25 8.7 ms

23:01:10.910 200.5 ms VRageWorker 2 762 25 9.6 ms

23:01:16.277 196.7 ms VRageWorker 6 762 24 10.9 ms

23:01:21.644 196.6 ms VRageWorker 0 761 24 9.0 ms

23:01:27.010 200.0 ms VRageWorker 0 753 25 9.3 ms

23:01:32.377 197.3 ms VRageWorker 9 745 24 9.4 ms

23:01:37.743 196.0 ms VRageWorker 1 762 24 9.7 ms

Control windows between stalls: 0 INF opens, input thread not blocked.

Every stall matched a freeze in the frame-by-frame video analysis.


NOT THE CAUSE

- Rendering: median 8.0 ms between presents, p99 10.7 ms (Independent Flip, V-Sync + G-Sync).

- Simulation: 3,600/min, worst ServerSessionTime under 3.6 ms, In-Process Server Thread never off-CPU for more than 15.8 ms.

- .NET: no GCs in the window, no contention events, GCStalls = 0.

- Other processes: a system-wide periodicity scan found nothing else on the 5.37 s rhythm.

- Same with the in-game 60 fps cap, the driver 141 fps cap, V-Sync on/off, Balanced/High performance power plans, and all background utilities closed.


WHY IT VARIES BY PC

Enumeration time grows with HID device count: about 750 INF opens per scan for 85 present entries, roughly 9 per device. This system has 69 HIDClass, 10 Keyboard and 6 Mouse entries (gaming keyboard and mouse, 2 wireless receivers, flight stick/throttle/pedals, USB mic, and the Intel HID Event Filter with 23 child entries), so players with gaming peripherals are hit hardest.


SUGGESTED FIX

- Don't hold the input path's lock (or wait on the result) during device enumeration. Run it fully asynchronously.

- Re-enumerate on device-change notifications (WM_DEVICECHANGE / GameInput callbacks) instead of on a timer.

- Cache device friendly names per instance instead of re-resolving them through INF parsing every time.


HOW TO CONFIRM

Elevated: wpr -start CPU -start FileIO -filemode, play about 30 s, wpr -stop se2.etl. In WPA, look for bursts of C:\Windows\INF\input.* / hidserv.* opens from VRageWorker threads, and long WrAlertByThreadId waits on the high-frequency input thread whose readying thread is that worker.


RELATED

With -captureSpikes:25,30,1,0,0,1 the log shows "Spike Profiling enabled" and 66 "requested save" lines, but no .opt files are ever written (Temp\Spikes stays empty, no error logged).

Replies (7)

photo
1

I removed every input device entry I could: I uninstalled the Intel HID Event Filter and unplugged two Logitech wireless receivers and a flight stick, throttle and rudder pedals. That took the system from 85 present input device entries to 32. Then I traced the game again.

The stall is still there on every scan, but it got shorter in proportion. The input thread is now blocked for 76.5–79.3 ms instead of 196–200 ms. That works out to about 2.3 ms per device entry, with almost no fixed cost. The mechanism hasn't changed: a VRageWorker thread enumerates input devices, opens Windows driver INF files, and releases the input thread when it finishes.

This can't be fixed on the player's side. The 32 remaining entries are hardware every player has (22 of them are just one gaming mouse and keyboard), and 77 ms is still about 5 frames at 60 Hz with no input. That's still clearly visible as a freeze followed by a jump.

photo
1

I'm sure it's obvious and doesn't need to be said but all the heavy lifting has been done with Claude Opus 5.5 and I've copy/pasted a lot of this in here directly. But I can assure you that my experience matches exactly what this data shows and currently after buying the game and spending over an hour trying to figure this out, It's unfortunately unplayable for me.


Hopefully with everything I've provided here it makes it much easier for you to resolve for everybody including myself.

Thanks.

photo
1

Yeah I don't know about all this , but I do have the dropped frame every few seconds.

photo
1

It's good to know that somebody else experiences the hitches because it's possible that many people experience this and just haven't questioned it. Due to it being an early access release.

I think my case was unique in that I have a lot of HID devices and therefor it accentuated the problem more to start with. But even now after reducing the issue as much as possible, it's still nauseating for me. But I am particularly susceptible to motion related issues.

photo
photo
1

Full transparency here. I'm not a programmer and I used Claude to do all of the heavy lifting here. I simply put in the effort to direct it as necessary. But I have solved this problem, and hopefully this will allow you (Keen) to implement an appropriate fix for everybody who may be affected.


UPDATE: cause located in the code, and confirmed with a test fix (game version 4.0.0.3169)


WHERE IT IS

VRage.Platform.Windows.dll, class Keen.VRage.Platform.Windows.Input.InputAPI.InputApi (Input\InputAPI\InputAPI.cs)


- UpdateInputApi() runs every input frame (through InputApi.Update()). Each time 5 s have passed since the last scan finished (DEVICE_POLL_TICKS = 50,000,000 Stopwatch ticks), it starts PollDevices(). The timestamp is set again when the scan finishes, which gives the 5.2–5.4 s period.

- PollDevices() hands PollState to a worker thread (Parallel.Schedule). PollState calls DirectInput.GetDevices(DeviceClass.All, AttachedOnly), then calls IsDeviceAttached() once for every device it already knows.

- Correction to my original post: the game's own code does not wait for this job (it uses SkipWait). The blocking happens inside DirectInput. Keyboard reads on the window thread (PollOnWindowThread > ReadState > Poll / GetCurrentState) and mouse reads both stall while EnumDevices runs on the worker. That fits the native lock wait (WrAlertByThreadId) seen in the ETW trace. Making the scan more asynchronous will not help, because it already runs in the background. The enumeration on a fixed 5 s timer is the problem.


HOW I CONFIRMED IT

I wrote a local test fix as a .NET startup hook. It does not change any game files or patch any methods. It lets the startup scan run, then holds _lastDevicePoll at long.MaxValue, the value the game already uses to mean "scan in progress", so the 5 s rescan never starts. It triggers a rescan only when Windows reports a HID, keyboard or mouse device being added or removed.


Test: same save and scene, two sessions, one with the fix off and one with it on. Each Steam recording is about 2.5 minutes, analysed for repeated (frozen) frames while the camera is moving.


Fix OFF:

- 1,132 repeated frames out of 7,085 moving frames (16.0%)

- 50 freezes of 2 or more frames, 19 of them lasting 3–11 frames. The median gap between those 19 freezes was 5.19 s, matching the scan period.


Fix ON:

- 7 repeated frames out of 7,135 moving frames (0.1%)

- No freezes of 2 or more frames

- The fix's log shows the one startup scan (288 ms) and no scans after it.


Both sessions had the same render rate (~125 fps), frame time (~7.9 ms) and 60 Hz simulation, according to the game's own STATISTICS log lines. Camera motion was similar in both recordings. The stutter is gone with the fix on.


One more observation: with the fix off there were also about 990 single repeated frames, spread irregularly between the big freezes. These also disappeared with the fix on. I have not worked out the mechanism for those.


SUGGESTED FIX

1. Remove the 5 s timer. Re-enumerate only when Windows reports a device change (WM_DEVICECHANGE with RegisterDeviceNotification, or CM_Register_Notification, for the HID, keyboard and mouse interface classes), debounced by about 1 s.

2. In PollState, work out removed devices from the GetDevices result instead of calling IsDeviceAttached() for each known device. That makes each scan cheaper.

3. Consider enumerating only the device classes you use. WinInputEngineComponent registers only Mouse and Keyboard, but the scan uses DeviceClass.All.


Notes: tested on one PC (32 HID entries at the time). The rescan-on-device-change path was not exercised during this test. Happy to provide the test fix source or the raw analysis if useful.

photo
1

Forwarding my own report of the bug here since i managed to fix it for myself : https://support.keenswh.com/spaceengineers2/pc/topic/58193-severe-camera-lag-on-mouse-input-bug

TLDR - While in game, remove your mouse from it's current USB port and switch to another one. For me, i swapped keyboard and mouse and it instantly fixed it.

photo
1

If it comes back and you aren't able to resolve it again before Keen addresses this. I have a more permanent fix. But I won't share it publicly.

photo
photo
1

Super big ups to @NewoNZ for figuring out what was causing this. I've been putting up with the awful sticky camera input hitches for 60hrs+ since purchase. Went in there and fixed it myself thanks to this post, took me like 30 seconds and now it runs smooth as butter. Kudos

photo
1

Glad to hear it.


Nobody should be putting up with something like this and I'm honestly surprised it hasn't been brought up by other people this far into development.


Maybe for the vast majority of people the hitch is so minor that its not noticeable.


Either way, the fix is simple and somebody really should get on top of this as soon as possible.

photo
photo
1

For me it's more subtle (definitely not 200ms; more like a skipped frame or two). But I only have a non-"gamer" trackball and keyboard.

Leave a Comment
 
Attach a file
You can't vote. Please authorize!
You can't vote. Please authorize!