You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With the default libuv thread pool size (4), I fire six crypto.pbkdf2() calls and one fs.readFile() of a small, already-cached file in the same synchronous tick. The six pbkdf2 calls are issued before the fs.readFile call. I expected the readFile callback to complete after the pbkdf2 work, but it consistently lands in the first wave of completions, while two of the pbkdf2 tasks are pushed to a later wave.
Environment
Node.js: v22.22.3
OS: macOS
UV_THREADPOOL_SIZE: unset (default 4)
Reproduction
constcrypto=require('crypto');constfs=require('fs');conststart=Date.now();constt=()=>`+${Date.now()-start}ms`;for(leti=1;i<=6;i++){crypto.pbkdf2('password','salt',100000,64,'sha512',()=>{console.log(`pbkdf2 ${i} done at ${t()}`);});}fs.readFile(__filename,()=>{console.log(`readFile done at ${t()}`);});
Observed output
pbkdf2 3 done at +22ms
pbkdf2 4 done at +24ms
pbkdf2 2 done at +24ms
pbkdf2 1 done at +24ms
readFile done at +24ms
pbkdf2 5 done at +42ms
pbkdf2 6 done at +42ms
The four-then-two split for the pbkdf2 tasks matches the 4-thread pool as expected.
Question
Given that all six pbkdf2 calls are submitted before fs.readFile in the same tick, why does the readFile callback complete in the first wave (~24ms) rather than after pbkdf2 5 and 6?
I'm trying to build an accurate mental model of how the thread pool orders and schedules this work. A pointer to where in the source this ordering is determined would be much appreciated. Thanks!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
With the default libuv thread pool size (4), I fire six
crypto.pbkdf2()calls and onefs.readFile()of a small, already-cached file in the same synchronous tick. The six pbkdf2 calls are issued before thefs.readFilecall. I expected thereadFilecallback to complete after the pbkdf2 work, but it consistently lands in the first wave of completions, while two of the pbkdf2 tasks are pushed to a later wave.Environment
UV_THREADPOOL_SIZE: unset (default 4)Reproduction
Observed output
The four-then-two split for the pbkdf2 tasks matches the 4-thread pool as expected.
Question
Given that all six pbkdf2 calls are submitted before
fs.readFilein the same tick, why does thereadFilecallback complete in the first wave (~24ms) rather than after pbkdf2 5 and 6?I'm trying to build an accurate mental model of how the thread pool orders and schedules this work. A pointer to where in the source this ordering is determined would be much appreciated. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions