native: Fix a race in select()

I also did a few small cleanups along the way (separated by commit)

Closes #13494