Better thread safety around handler registration