firebase-tools
firebase-tools copied to clipboard
Fix non-resolving promises bug
Description
Fixes a bug (#3915) where promises would not resolve on rapid calls to the emulator.
The root case appears to be that in Node, there is a limitation to spawned subprocesses in regards to pipes. The pipes "have limited (and platform-specific) capacity". My theory is that the rate at which we issues requests to the rules binary via piping and the subsequent return via pipes exceeds some limit and we're not catching all the returns in the this._childprocess.stderr?.on("data" portion of runtime.ts. This causes the resultant output to not be captured by the parent process and subsequently be printed out to the console.
There's probably also some issues with the fact that if the subprocess hits a limit in regards to writing to the buffer and it not being picked up by the parent process, the subprocess blocks as it waits for the pipe to accept more data.
The solution proposed in this PR is to introduce a lock on the Child Process that is unlocked after a brief time (15ms worked well in testing, much lower and we run back into the same issue). This, from testing, is enough time for the parent process to pick up anything in the pipes and get ready for the next output.
This PR also updates cross-spawn to 7.0.3 as there were some fixes in 7.0.2 that supposedly addressed some threading issues, patched just to be safe
Alternatives Tested
Tried a few different approaches that ended up resulting in behavior not changing. Some of these include spawning the child process synchronously and issuing a delay to all incoming requests. In the end, I think locking this resource and guaranteeing unobstructed access to the ChildProcess is the best solution.
Scenarios Tested
Manually tests against emulator build and the issue no longer comes up after this fix. Added tests.