Class SharedDeflaterDetector

java.lang.Object
se.deversity.asynctest.diagnostics.SharedDeflaterDetector

public final class SharedDeflaterDetector extends Object
Detects Deflater / Inflater instances shared across threads.

Why it matters. Both classes wrap a native zlib stream whose state machine (setInput then deflate/inflate until finished) is advanced by every call, which the javadoc describes and which is the whole of the argument here - the JDK states no thread-safety contract for either class. A state machine advanced from two threads at once is not in a state either of them can reason about. Unsynchronized concurrent use of one instance interleaves bytes from different logical streams, producing corrupt/undecompressable output or a NullPointerException from the native layer once one thread calls end() while another is mid-stream.

There is a second hazard: Deflater/Inflater hold off-heap memory that is only released by end(). Sharing one instance encourages "someone else will close it" ownership confusion, which is also a resource leak — but the thread-safety violation is the more acute bug, so findings are reported here rather than via ResourceLeakDetector.

Synchronization awareness is partial. An access recorded while the accessing thread holds the instance's own monitor - the synchronized (deflater) idiom - counts as guarded, and an instance whose every access was guarded produces no finding. A guard on any other lock counts once the test declares it with AsyncTestContext.holdingLock(...) or the agent sees it taken. A lock that was never declared is invisible and still fires; treat such a finding as a prompt to verify the synchronization, or to move to a per-thread instance.

The safe pattern is one instance per thread (and a matching end() in a finally), or a fresh instance per compression unit.

Usage:


 var d = new SharedDeflaterDetector();
 d.recordAccess(deflater, "response-gzip", Thread.currentThread());
 d.recordAccess(inflater, "request-gunzip", Thread.currentThread());
 
Since:
1.7.0
  • Constructor Details

    • SharedDeflaterDetector

      public SharedDeflaterDetector()
  • Method Details

    • recordAccess

      public void recordAccess(Deflater deflater, String name, Thread thread)
      Record an access to a Deflater instance.
      Parameters:
      deflater - the Deflater (null-safe)
      name - descriptive label for reports (may be null)
      thread - accessing thread
    • recordAccess

      public void recordAccess(Inflater inflater, String name, Thread thread)
      Record an access to an Inflater instance.
      Parameters:
      inflater - the Inflater (null-safe)
      name - descriptive label for reports (may be null)
      thread - accessing thread
    • analyze

      Analyses what has been recorded about the observation and builds the report for it.
      Returns:
      the findings this detector collected during the run