Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed race condition: SocketException due to mySock closed while sending response #174

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions src/gov/nist/javax/sip/stack/TCPMessageChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ protected synchronized void sendMessage(byte[] msg, boolean isClient) throws IO

Socket sock = null;
IOException problem = null;
// try to prevent at least the worst thread safety issues by using a local variable ...
Socket mySockLocal = mySock;

try {
sock = this.sipStack.ioHandler.sendBytes(this.messageProcessor.getIpAddress(),
this.peerAddress, this.peerPort, this.peerProtocol, msg, isClient, this);
Expand Down Expand Up @@ -270,16 +273,16 @@ protected synchronized void sendMessage(byte[] msg, boolean isClient) throws IO
// if (mySock == null && s != null) {
// this.uncache();
// } else
if (sock != mySock && sock != null) {
if (mySock != null) {
if (sock != mySockLocal && sock != null) {
if (mySockLocal != null) {
if(logger.isLoggingEnabled(LogWriter.TRACE_WARN)) {
logger.logWarning(
"Old socket different than new socket on channel " + key);
logger.logStackTrace();
logger.logWarning(
"Old socket local ip address " + mySock.getLocalSocketAddress());
"Old socket local ip address " + mySockLocal.getLocalSocketAddress());
logger.logWarning(
"Old socket remote ip address " + mySock.getRemoteSocketAddress());
"Old socket remote ip address " + mySockLocal.getRemoteSocketAddress());
logger.logWarning(
"New socket local ip address " + sock.getLocalSocketAddress());
logger.logWarning(
Expand All @@ -288,19 +291,23 @@ protected synchronized void sendMessage(byte[] msg, boolean isClient) throws IO
close(false, false);
}
if(problem == null) {
if(mySock != null) {
if(logger.isLoggingEnabled(LogWriter.TRACE_WARN)) {
logger.logWarning(
"There was no exception for the retry mechanism so creating a new thread based on the new socket for incoming " + key);
}
}
mySock = sock;
this.myClientInputStream = mySock.getInputStream();
this.myClientOutputStream = mySock.getOutputStream();
Thread thread = new Thread(this);
thread.setDaemon(true);
thread.setName("TCPMessageChannelThread");
thread.start();
if (mySockLocal != null) {
if(logger.isLoggingEnabled(LogWriter.TRACE_WARN)) {
logger.logWarning(
"There was no exception for the retry mechanism so creating a new thread based on the new socket for incoming " + key);
}
}
// NOTE: need to consider refactoring the whole socket handling with respect to thread safety
if (mySockLocal == mySock) {
// still not thread safe :-( but what else to do?
mySock = sock;
this.myClientInputStream = mySock.getInputStream();
this.myClientOutputStream = mySock.getOutputStream();
Thread thread = new Thread(this);
thread.setDaemon(true);
thread.setName("TCPMessageChannelThread");
thread.start();
}
} else {
if(logger.isLoggingEnabled(LogWriter.TRACE_WARN)) {
logger.logWarning(
Expand Down
20 changes: 13 additions & 7 deletions src/gov/nist/javax/sip/stack/TLSMessageChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ protected synchronized void sendMessage(byte[] msg, boolean retry) throws IOExce
}
Socket sock = null;
IOException problem = null;
// try to prevent at least the worst thread safety issues by using a local variable ...
Socket mySockLocal = mySock;
try {
sock = this.sipStack.ioHandler.sendBytes(
this.getMessageProcessor().getIpAddress(), this.peerAddress, this.peerPort,
Expand Down Expand Up @@ -278,18 +280,22 @@ protected synchronized void sendMessage(byte[] msg, boolean retry) throws IOExce
close(false, false);
}
if(problem == null) {
if(mySock != null) {
if (mySockLocal != null) {
if(logger.isLoggingEnabled(LogWriter.TRACE_WARN)) {
logger.logWarning(
"There was no exception for the retry mechanism so creating a new thread based on the new socket for incoming " + key);
}
}
mySock = sock;
this.myClientInputStream = mySock.getInputStream();
Thread thread = new Thread(this);
thread.setDaemon(true);
thread.setName("TCPMessageChannelThread");
thread.start();
// NOTE: need to consider refactoring the whole socket handling with respect to thread safety
if (mySockLocal == mySock) {
// still not thread safe :-( but what else to do?
mySock = sock;
this.myClientInputStream = mySock.getInputStream();
Thread thread = new Thread(this);
thread.setDaemon(true);
thread.setName("TCPMessageChannelThread");
thread.start();
}
} else {
if(logger.isLoggingEnabled(LogWriter.TRACE_WARN)) {
logger.logWarning(
Expand Down