Skip to content

Commit

Permalink
JSSSession references JSSEngineReferenceImpl which references JSSSession
Browse files Browse the repository at this point in the history
Break the cycle so the garbage collector doesn't need to figure out it can
release these together and then let the JSSEngineReferenceImpl.finalize() run.
  • Loading branch information
uplogix-mmcclain committed Jan 9, 2023
1 parent 028573c commit 830e7bd
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions base/src/main/java/org/mozilla/jss/ssl/javax/JSSSession.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.mozilla.jss.ssl.javax;

import java.lang.AutoCloseable;
import java.lang.ref.WeakReference;
import java.security.cert.Certificate;
import javax.security.cert.X509Certificate;
import java.security.Principal;
Expand All @@ -14,7 +15,7 @@

public class JSSSession implements SSLSession, AutoCloseable {
private static final int MAX_TLS_RECORD_PAYLOAD = (1 << 14);
private JSSEngine parent;
private WeakReference<JSSEngine> parent;

private int applicationBufferSize;
private int packetBufferSize;
Expand Down Expand Up @@ -42,7 +43,7 @@ public class JSSSession implements SSLSession, AutoCloseable {
private boolean closed;

protected JSSSession(JSSEngine engine, int buffer_size) {
this.parent = engine;
this.parent = new WeakReference<>(engine);

applicationBufferSize = Math.min(buffer_size, MAX_TLS_RECORD_PAYLOAD);
packetBufferSize = buffer_size;
Expand All @@ -51,11 +52,15 @@ protected JSSSession(JSSEngine engine, int buffer_size) {
}

public JSSEngine getEngine() {
return parent;
return parent.get();
}

public SSLChannelInfo getChannelInfo() {
SSLFDProxy ssl_fd = parent.getSSLFDProxy();
JSSEngine jssEngine = parent.get();
if (jssEngine == null) {
return null;
}
SSLFDProxy ssl_fd = jssEngine.getSSLFDProxy();
if (ssl_fd != null && ssl_fd.handshakeComplete) {
return SSL.GetChannelInfo(ssl_fd);
}
Expand All @@ -64,8 +69,12 @@ public SSLChannelInfo getChannelInfo() {
}

public SSLPreliminaryChannelInfo getPreliminaryChannelInfo() {
if (parent.getSSLFDProxy() != null) {
return SSL.GetPreliminaryChannelInfo(parent.getSSLFDProxy());
JSSEngine jssEngine = parent.get();
if (jssEngine == null) {
return null;
}
if (jssEngine.getSSLFDProxy() != null) {
return SSL.GetPreliminaryChannelInfo(jssEngine.getSSLFDProxy());
}

return null;
Expand Down Expand Up @@ -149,8 +158,12 @@ public boolean isValid() {

@Override
public void invalidate() {
if (parent.getSSLFDProxy() != null) {
SSL.InvalidateSession(parent.getSSLFDProxy());
JSSEngine jssEngine = parent.get();
if (jssEngine == null) {
return;
}
if (jssEngine.getSSLFDProxy() != null) {
SSL.InvalidateSession(jssEngine.getSSLFDProxy());
}
}

Expand Down

0 comments on commit 830e7bd

Please sign in to comment.