Fix legacy query race condition (#3131)