Closed kgpax closed 8 months ago
Latest commit: 4d9746b20e3e34b4865251d808b8a0beb512c485
The changes in this PR will be included in the next version bump.
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR
What does this PR do
What was the cause of the issue?
There is code in
/packages/nextjs/src/tracing.ts
which invokes the@envyjs/node
enableTracing
function:This code looks like it should prevent the call to
nodeTracing
happening more than once; however I could put aconsole.log
inside that condition and observe that on some occasions (such as reloading the web page of the app being traced, or by making a code change triggering HMR) I would see this being logged twice, therefore callingnodeTracing
twice.Once this happened, requests made by API routes started to appear duplicated in Envy.
What did I try?
1. Mutex locking
First I tried adding locking to the code to prevent the code from executing when it was locked. I used the
async-mutex
package to do something like this:👎 However, this didn't seem to make a difference - it was still possible for this registration to happen twice.
2. Debouncing
Then I tried debouncing the call, such that duplicate attempts to make the connect would cancel the previous attempt(s) if fast enough...
👎 Still, this didn't prevent the issue.
3. Node global flag
Based on the above, I concluded that perhaps the problem is that this
packages/nextjs/src/tracing
module is being loaded as two separate instances, meaning that theinitialized
flag (or themutex
, or the debounced function) scoped to that module is only useful for code re-using that instance, and not for code spawning another instance. What we needed was a more guaranteed global scope for this flag. Therefore I changed to use the nodeglobal
object to store the flag indicating that we have initialized:🎉 This seems to work, and I've tested it on the project that I originally observed the bug on by monkey-patching this fix.
Fixes #200