Fix key gen race condition, fix #17289
Merge pull request #18350 from thatch45/key_race

Fix key gen race condition, fix #17289