Cherry-pick: More fixes related to the random crash fix.

Change-Id: I8f6bf7484042db1d3f5c44144bf4c9eedc6b5f1c
Merge "Cherry-pick: More fixes related to the random crash fix." into v4.0.1-rc