fix usage of scoped locks

Previously they were getting destroyed immediately because they were not stored in a temporary scope variable.

This was reported by someone else, but I'll leave them anonymous for now and let them identify themselves if they like.