ChatGPT解决这个技术问题 Extra ChatGPT

Official reasons for "Software caused connection abort: socket write error"

Given this stack trace snippet

Caused by: java.net.SocketException: Software caused connection abort: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method)

I tried to answer the following questions:

What code is throwing this exception? (JVM?/Tomcat?/My code?) What causes this exception to be thrown?

Regarding #1:

Sun's JVM source doesn't contain this exact message, but I think the text Software caused connection abort: socket write error is from the native implementation of SocketOutputStream:

private native void socketWrite0(FileDescriptor fd, byte[] b, int off,
                 int len) throws IOException;

Regarding #2

My guess is that it is caused when the client has terminated the connection, before getting the full response (e.g. sent a request, but before getting the full response, it got closed / terminated / offline)

Questions:

Are the above assumptions correct (#1 and #2)? Can this be diffrentiated from the situation: "could not write to the client, due to a network error on the server side"? or would that render the same error message? And most important: Is there an official document (e.g from Sun) stating the above?

I need to have a proof that this stack trace is the socket client's "fault", and there is nothing that the server could have done to avoid it. (except catching the exception, or using a non Sun JVM SocketOutputStream, though both don't really avoid the fact the client has terminated)

I have this issue when cancelling a download with Firefox
Hey Eran I am also getting this exception while sending/writing (outs.write(audioBytes);) byte[] in to OutputStream. When audio is plying and while playing if user clicks on any other menu (which send an server request) I got the same error on console. so is it safe to ignore this exception?
@Amogh - It seems so, yes. Basically from what the answers describe, this is a Windows specific error, but I assume on Linux you'll get the same exception just with a different wording... (My laymen terms understanding of it is basically that this is caused when you send via a socket to some remote location X and X got disconnected in the middle, but I'm sure it's not the most accurate way to describe it)
For me this happened when the database server was restarted and the application was still trying to query using connections previously opened. Not sure why these were not refreshed as we're using DBCP based pooling. But restarting the application fixed the problem.

A
Alireza Noorali

This error can occur when the local network system aborts a connection, such as when WinSock closes an established connection after data retransmission fails (receiver never acknowledges data sent on a datastream socket).

See this MSDN article. See also Some information about 'Software caused connection abort'.


@MatGessel That article just repeats the confusion, and adds some of its own. WSAECONNABORTED is a Winsock error code, so there cannot possibly be a Berkeley explanation for it. The situation described about the HTTP server would produce ECONNRESET, not WSAECONNABORTED.
@rustyx All three sources cited here state that it is produced by ACK failures. If you have a source for your own claim please cite it.
This is not a real answer as it doesn't give you information to pursue the problem further. The answer here is basically "something bad happened on the network". It would be really helpful to understand what further logs and other records of activity could allow me to pinpoint the underlying issue.
@DerekBennet The underlying issue is stated in this answer. Your comment is baffling.
C
Community

The java.net.SocketException is thrown when there is an error creating or accessing a socket (such as TCP). This usually can be caused when the server has terminated the connection (without properly closing it), so before getting the full response. In most cases this can be caused either by the timeout issue (e.g. the response takes too much time or server is overloaded with the requests), or the client sent the SYN, but it didn't receive ACK (acknowledgment of the connection termination). For timeout issues, you can consider increasing the timeout value.

The Socket Exception usually comes with the specified detail message about the issue.

Example of detailed messages:

Software caused connection abort: recv failed. The error indicates an attempt to send the message and the connection has been aborted by your server. If this happened while connecting to the database, this can be related to using not compatible Connector/J JDBC driver. Possible solution: Make sure you've proper libraries/drivers in your CLASSPATH.

Software caused connection abort: connect. This can happen when there is a problem to connect to the remote. For example due to virus-checker rejecting the remote mail requests. Possible solution: Check Virus scan service whether it's blocking the port for the outgoing requests for connections.

Software caused connection abort: socket write error. Possible solution: Make sure you're writing the correct length of bytes to the stream. So double check what you're sending. See this thread.

Connection reset by peer: socket write error / Connection aborted by peer: socket write error The application did not check whether keep-alive connection had been timed out on the server side. Possible solution: Ensure that the HttpClient is non-null before reading from the connection.E13222_01

Connection reset by peer. The connection has been terminated by the peer (server).

Connection reset. The connection has been either terminated by the client or closed by the server end of the connection due to request with the request. See: What's causing my java.net.SocketException: Connection reset?


Only one of these 6 points actually answers the question, incorrectly. Several others are incorrect as well. The application can't 'check whether keep-alive connection had been timed out on the server side.' The HttpClient being null cannot possibly cause a SocketException. Not writing the correct length to the stream doesn't either.
a
akjoshi

I have seen this most often when a corporate firewall on a workstation/laptop gets in the way, it kills the connection.

eg. I have a server process and a client process on the same machine. The server is listening on all interfaces (0.0.0.0) and the client attempts a connection to the public/home interface (note not the loopback interface 127.0.0.1).

If the machine is has its network disconnected (eg wifi turned off) then the connection is formed. If the machine is connected to the corporate network (directly or vpn) then the connection is formed.

However, if the machine is connected to a public wifi (or home network) then the firewall kicks in an kills the connection. In this situation connecting the client to the loopback interface works fine, just not to the home/public interface.

Hope this helps.


Firewalls prevent connections. The question is about resetting an existing connection.
s
stacker

To prove which component fails I would monitor the TCP/IP communication using wireshark and look who is actaully closing the port, also timeouts could be relevant.


Nobody is closing the port. The operating system is aborting the connection.
@EJP I've seen this happen when the gets overloaded and runs out of memory. I am not sure it is the OS that closes the connection but JVM goes wild.
@Zee There is a difference between closing a port, which is visible as a FIN in Wireshark, and aborting the connection, which isn't.
P
PRO_gramista

For anyone using simple Client Server programms and getting this error, it is a problem of unclosed (or closed to early) Input or Output Streams.


No it isn't. That would consitute a socket leak, which would eventually cause an FD exhaustion.
B
Brian Agnew

Have you checked the Tomcat source code and the JVM source ? That may give you more help.

I think your general thinking is good. I would expect a ConnectException in the scenario that you couldn't connect. The above looks very like it's client-driven.


Yes, I've checked. Tomcat's sources didn't contain any permutation of the sentence, thanks.
No he hasn't checked the Tomcat source AND the JVM source.
Or if he has checked the JVM source, he has not checked all of it.
@Ehrann - the message string is most likely in the native sources. But you should also check the event log. IMO, the latter is likely to be more informative.
This message string comes from the operating system actually.
N
Nirav Chhatrola

I was facing the same issue. Commonly This kind of error occurs due to client has closed its connection and server still trying to write on that client. So make sure that your client has its connection open until server done with its outputstream. And one more thing, Don`t forgot to close input and output stream.

Hope this helps. And if still facing issue than brief your problem here in details.


@BhavinChhatrola No, an incorrect answer. The situation described produces 'connection reset by peer', not the error in the question.
J
JamesKDell

Had an SSLPoke.bat (SSL troubleshooting script) window script that was getting this error despite importing the correct certificates into the cacerts trustore.

C:\Java\jdk1.8.0_111\jre\lib\security>SSLPoke.bat

C:\Java\jdk1.8.0_111\jre\lib\security>"C:\jdk1.8.0_101\jre\bin\java" 
     `SSLPoke  tfs.corp.****.com  443`

java.net.SocketException: Software caused connection abort: recv failed
    `at java.net.SocketInputStream.socketRead0(Native Method)`
    `at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)`
    `at java.net.SocketInputStream.read(SocketInputStream.java:170)`
    `at java.net.SocketInputStream.read(SocketInputStream.java:141)`
    `at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)`
    `at sun.security.ssl.InputRecord.read(InputRecord.java:503)`
    `at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)`
    `at sun.security.ssl.SSLSocketImpl.performInitialHandshake
       (SSLSocketImpl.java:1375)`
    `at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:747)`
    `at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123)`
    `at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:138)`
    `at SSLPoke.main(SSLPoke.java:28)`

I then checked some old notes about some network changes at my job. We would need in some cases to add the JVM parameter -Djava.net.preferIPv4Stack=true to make connections to certain machines in our network to avoid this error.

C:\Java\jdk1.8.0_111\jre\lib\security>"C:\Java\jdk1.8.0_111\bin\java"  
    **-Djava.net.preferIPv4Stack=true**  SSLPoke tfs.corp.****.com 443

Successfully connected

The code for SSLPoke can be downloaded from here: https://gist.github.com/4ndrej/4547029


M
Marco

This error happened to me while testing my soap service with SoapUI client, basically I was trying to get a very big message (>500kb) and SoapUI closed the connection by timeout.

On SoapUI go to: File-->Preferences--Socket Timeout(ms)

...and put a large value, such as 180000 (3 minutes), this won't be the perfect fix for your issue because the file is in fact to large, but at least you will have a response.


m
manuelvigarcia

Closed connection in another client

In my case, the error was:

java.net.SocketException: Software caused connection abort: recv failed

It was received in eclipse while debugging a java application accessing a H2 database. The source of the error was that I had initially opened the database with SQuirreL to check manually for integrity. I did use the flag to enable multiple connections to the same DB (i.e. AUTO_SERVER=TRUE), so there was no problem connecting to the DB from java.

The error appeared when, after a while --it is a long java process-- I decided to close SQuirreL to free resources. It appears as if SQuirreL were the one "owning" the DB server instance and that it was shut down with the SQuirreL connection.

Restarting the Java application did not yield the error again.

config

Windows 7

Eclipse Kepler

SQuirreL 3.6

org.h2.Driver ver 1.4.192


l
lrnzcig

In the situation explained below, client side will throw such an exception:

The server is asked to authenticate client certificate, but the client provides a certificate which Extended Key Usage doesn't support client auth, so the server doesn't accept the client's certificate, and then it closes the connection.


This answer is incorrect. In the case you describe an SSLException will be thrown.
actually it throw SocketException just like the current question , i had tested
S
Sefi Erlich

My server was throwing this exception in the pass 2 days and I solved it by moving the disconnecting function with:

outputStream.close();
inputStream.close();
Client.close();

To the end of the listing thread. if it will helped anyone.


E
Elloco

In my case, I developped the client and the server side, and I have the exception :

Cause : error marshalling arguments; nested exception is: java.net.SocketException: Software caused connection abort: socket write error

when classes in client and server are different. I don't download server's classes (Interfaces) on the client, I juste add same files in the project. But the path must be exactly the same. For example, on the server project I have java\rmi\services packages with some serviceInterface and implementations, I have to create the same package on the client project. If I change it by java/rmi/server/services for example, I get the above exception. Same exception if the interface version is different between client and server (even with an empty row added inadvertently ... I think rmi makes a sort of hash of classes to check version ... I don't know... If it could help ...


B
Bojan B

I was facing the same problem with wireMock while mocking the rest API calls. Earlier I was defining the server like this:

WireMockServer wireMockServer = null;

But it should be defined like as shown below:

@Rule 
public WireMockRule wireMockRule = new WireMockRule(8089);

That would cause a NullPointerException, not this problem.

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now