Protect against possible race conditions

Test: make cts -j123 && cts-tradefed run cts-dev -m \
CtsMediaTestCases --compatibility:module-arg \
CtsMediaTestCases:include-annotation:\
android.platform.test.annotations.RequiresDevice

Test: Run the poc in b/38234812

Bug: 38234812
Change-Id: I28322e5a812c02c8e449ab8773715f60b9f5d976
(cherry picked from commit f9c81464b9494082fa7cdff9f70c5402e8ac78c0)
4 files changed