@@ -1967,6 +1967,30 @@ public synchronized boolean sessionResumed() throws SSLException {
19671967 return false ;
19681968 }
19691969
1970+ /**
1971+ * Internal private method to check if WolfSSLInputStream
1972+ * and WolfSSLOutputStream are closed.
1973+ *
1974+ * @return true if both streams are closed (or were not created/null),
1975+ * otherwise false if they were created and are still active.
1976+ */
1977+ private boolean ioStreamsAreClosed () {
1978+
1979+ if (this .inStream == null && this .outStream == null ) {
1980+ return true ;
1981+ }
1982+
1983+ if (this .inStream != null && !this .inStream .isClosed ()) {
1984+ return false ;
1985+ }
1986+
1987+ if (this .outStream != null && !this .outStream .isClosed ()) {
1988+ return false ;
1989+ }
1990+
1991+ return true ;
1992+ }
1993+
19701994 /**
19711995 * Closes this SSLSocket.
19721996 *
@@ -2132,13 +2156,22 @@ public synchronized void close() throws IOException {
21322156
21332157 /* Connection is closed, free native WOLFSSL session
21342158 * to release native memory earlier than garbage
2135- * collector might with finalize(), if we don't
2136- * have any threads still waiting in poll/select. */
2137- if (this .ssl .getThreadsBlockedInPoll () == 0 ) {
2159+ * collector might with finalize(), Don't free if we
2160+ * have threads still waiting in poll/select or if
2161+ * our WolfSSLInputStream or WolfSSLOutputStream are
2162+ * still open. */
2163+ if (this .ssl .getThreadsBlockedInPoll () == 0 &&
2164+ ioStreamsAreClosed ()) {
21382165 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
21392166 () -> "calling this.ssl.freeSSL()" );
21402167 this .ssl .freeSSL ();
21412168 this .ssl = null ;
2169+ } else {
2170+ WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
2171+ () -> "deferring freeing this.ssl, threads " +
2172+ "blocked in poll: " +
2173+ this .ssl .getThreadsBlockedInPoll () +
2174+ ", or streams not closed" );
21422175 }
21432176
21442177 /* Reset internal WolfSSLEngineHelper to null */
@@ -2517,7 +2550,7 @@ class WolfSSLInputStream extends InputStream {
25172550
25182551 private WolfSSLSession ssl ;
25192552 private WolfSSLSocket socket ;
2520- private boolean isClosed = true ;
2553+ private volatile boolean isClosed = true ;
25212554
25222555 /* Atomic boolean to indicate if this InputStream has started to
25232556 * close. Protects against deadlock between two threads calling
@@ -2530,6 +2563,18 @@ public WolfSSLInputStream(WolfSSLSession ssl, WolfSSLSocket socket) {
25302563 this .isClosed = false ;
25312564 }
25322565
2566+ /**
2567+ * Non standard method to check if this InputStream has been
2568+ * closed. This is used by WolfSSLSocket to check if the associated
2569+ * WolfSSLInputStream has been closed before calling freeSSL()
2570+ * internally.
2571+ *
2572+ * @return true if this InputStream has been closed, otherwise false
2573+ */
2574+ public boolean isClosed () {
2575+ return this .isClosed ;
2576+ }
2577+
25332578 /**
25342579 * Close InputStream, but gives caller option to close underlying
25352580 * Socket or not.
@@ -2741,7 +2786,7 @@ class WolfSSLOutputStream extends OutputStream {
27412786
27422787 private WolfSSLSession ssl ;
27432788 private WolfSSLSocket socket ;
2744- private boolean isClosed = true ;
2789+ private volatile boolean isClosed = true ;
27452790
27462791 /* Atomic boolean to indicate if this InputStream has started to
27472792 * close. Protects against deadlock between two threads calling
@@ -2754,6 +2799,18 @@ public WolfSSLOutputStream(WolfSSLSession ssl, WolfSSLSocket socket) {
27542799 this .isClosed = false ;
27552800 }
27562801
2802+ /**
2803+ * Non standard method to check if this OutputStream has been
2804+ * closed. This is used by WolfSSLSocket to check if the associated
2805+ * WolfSSLOutputStream has been closed before calling freeSSL()
2806+ * internally.
2807+ *
2808+ * @return true if this OutputStream has been closed, otherwise false
2809+ */
2810+ public boolean isClosed () {
2811+ return this .isClosed ;
2812+ }
2813+
27572814 /**
27582815 * Close OutputStream, but gives caller option to close underlying
27592816 * Socket or not.
0 commit comments