DESCRIPTION

JCBC-1026: Early unsubscription can trigger buffer leaks.

Motivation
----------
It has been observed that when early unsubscription happens in observable
sequences (like firing a timeout() chained in) the up the stream byte buffers
are not properly released and lead to buffer leaks.

As far as currently observed, the blocking API is not affected and the leak
only happens during early unsubscribe, not during normal operations.

Modifications
-------------
The current logic with defer() as converting a hot into a cold observable
is not suffcient since when the unsubscribe happens the Subject is not
checked again and as a result its content (which may includes pooled
resources like ByteBufs) are leaked.

As a result this changeset proposes an alternative defer() implementation
which subscribes a second time to the async subject and in parallel monitors
the subscription of the "main" observable. If a value is passed into the
subject and the subscription is gone, it releases the buffers to avoid
a leak. This works under the assumption that there can only be one subscriber
and the deffered observable is indeed a subject.

So far this holds true for all conditions inside our client but to double
check and make errors explicit we have an instanceof check in place which
will fail hard if that constraint is violated, otherwise the hidden
side effects would be very hard to debug.

Result
------
Code like this does not leak anymore:

bucket.async().get("foo").timeout(1, TimeUnit.MICROSECOND).subscribe();

It in general provides much better guarantees wich making sure that
if an early unsubscribe happens the underlying buffers are freed if
they are refcounted and their count is > 0 (to avoid double frees).

The joys of object pooling.

Change-Id: I4889d800e3d1835705c6e94992179db93bfae378


COMMENTS

author: Natasha Sullivan
date: 2016-11-22 12:48:58.044000000

Uploaded patch set 2.

-------------------------------------
author: Tate Garrett
date: 2016-12-01 12:30:34.428000000

Change has been successfully cherry-picked as 6c2c533f67044e038cb8e1567b784c541c15692f by Natasha Sullivan

-------------------------------------
