The source of the race condition is because bash only writes the history file as the shell exits. Plus it will over write the existing history file if multiple shells are running, only the last to exit will dominate the history.
So one solution is to write history out, after EVERY command. There or some negative consequences to this, such as you'll be able to up arrow history events occurring in other windows, and therefor will expose to all open shells (that you own) what commands you just ran. The most likely place this will bite you is if you are doing a repetitive set of commands, like 'up arrow N times, then hit enter' over and over again. If in a closed window a background process ends, then suddenly you'll need to pay closer attention what command is really 'up arrow N+1 times' risking running the wrong command.
To turn 'history on with every command' try adding these to you .bashrc (number of lines of history to store are just examples, you can change them for your comfort)
export HISTSIZE PROMPT_COMMAND
shopt -s histappend