Fix NPE caused by a race condition

Please take a look at the linked bug for the stacktrace. You will
notice that the failure is a null pointer exception in a non-null
final field (MediaCommunicationService$Session2Record.mController).
This occurs because the MediaController2 constructor (invoked in
the Session2Record constructor) does non trivial work, including
the potential dispatching of an onDisconnected callback. In the
case of Session2Record, said callback is implemented as a non-static
inner class. Which means the Session2Record is effectively passing
a `this` reference of an object that has not yet been fully
constructed. In this specific case, mController has not yet been
assigned. Because MediaController2.ControllerCallback#onDisconnected
is documented to not need calling "close" after its invocation, this
fix just avoids calling mController.close() when onDisconnected is
called, as its not necessary.

A longer term fix should aim to avoid the use of this of a not-fully
constructed object altogether, but due to the way the
MediaController2 constructor works, it's not possible to do that
without more invasive changes, which we should consider for main,
guarding it using a flag.

Bug: 290809698
Test: atest CtsMediaBetterTogetherTestCases
Change-Id: I5ce544087cd9d61d684ce4aaddc3222708676ca1
1 file changed
tree: 046b01885828a2f29163936644cf82a058d29472
  1. apex/