Lock order fixes cherry picked from master

- bed323cebcfcf3298002ea8bc2eb6787419043b6
- 580eb9424c5a9645c44b933e2ef144301af035cb
- e6e40842d15850de35d00d5138fcaac858e37adc
- ffaa2849e5afc573c4b0d82086b233f4675bed85