TCP Connection Hanging on SYN_SENT

Consider the following client and server components:

import java.io.InputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class client {
    public static void main(String[] args) throws IOException {
        while (true) {
            URL url = new URL("https://localhost:8000");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            int statusCode = connection.getResponseCode();
            System.out.println("Status Code: " + statusCode);
            connection.disconnect();
        }
    }
}
import java.io.OutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8000);
        while (true) {
            Socket clientSocket = serverSocket.accept();
            OutputStream outputStream = clientSocket.getOutputStream();
            outputStream.write("HTTP/1.1 200 OKrnContent-Length: 0rnrn".getBytes());
            outputStream.flush();
            clientSocket.close();
        }
    }
}

Upon running the client while the server is running, you will soon see that the client begins to hang on SYN_SENT on the TCP-level (for about 30 seconds in all):

$ watch -n 0.1 "ss -on state syn-sent '( dport = :8000 )'"
Every 0.1s: ss -on state syn-sent '( dport = :8000 )'               myhost: Tue Jul 16 04:08:52 2024

Netid Recv-Q Send-Q      Local Address:Port        Peer Address:Port           Process
tcp   0      1           [::ffff:127.0.0.1]:60418  [::ffff:127.0.0.1]:8000     timer:(on,3.731ms,2)
$ pkill -3 java
# Stack trace of client's main thread while hanging outputted in Java terminal...
"main" #1 prio=5 os_prio=0 cpu=2429.68ms elapsed=40.96s tid=0x000079e6c40266c0 nid=0x18a1c6 runnable  [0x000079e6cb9fd000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.Net.connect0([email protected]/Native Method)
    at sun.nio.ch.Net.connect([email protected]/Net.java:579)
    at sun.nio.ch.Net.connect([email protected]/Net.java:568)
    at sun.nio.ch.NioSocketImpl.connect([email protected]/NioSocketImpl.java:593)
    at java.net.Socket.connect([email protected]/Socket.java:633)
    at java.net.Socket.connect([email protected]/Socket.java:583)
    at sun.net.NetworkClient.doConnect([email protected]/NetworkClient.java:183)
    at sun.net.www.http.HttpClient.openServer([email protected]/HttpClient.java:533)
    at sun.net.www.http.HttpClient.openServer([email protected]/HttpClient.java:638)
    at sun.net.www.http.HttpClient.<init>([email protected]/HttpClient.java:281)
    at sun.net.www.http.HttpClient.New([email protected]/HttpClient.java:386)
    at sun.net.www.http.HttpClient.New([email protected]/HttpClient.java:422)
    at sun.net.www.protocol.http.HttpURLConnection.setNewClient([email protected]/HttpURLConnection.java:831)
    at sun.net.www.protocol.http.HttpURLConnection.setNewClient([email protected]/HttpURLConnection.java:819)
    at sun.net.www.protocol.http.HttpURLConnection.writeRequests([email protected]/HttpURLConnection.java:759)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0([email protected]/HttpURLConnection.java:1708)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream([email protected]/HttpURLConnection.java:1611)
    at java.net.HttpURLConnection.getResponseCode([email protected]/HttpURLConnection.java:529)
    at client.main(client.java:13)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0([email protected]/Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke([email protected]/NativeMethodAccessorImpl.java:77)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke([email protected]/DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke([email protected]/Method.java:568)
    at com.sun.tools.javac.launcher.Main.execute([email protected]/Main.java:419)
    at com.sun.tools.javac.launcher.Main.run([email protected]/Main.java:192)
    at com.sun.tools.javac.launcher.Main.main([email protected]/Main.java:132)

I’m building a Java application (I was attempting a 10 ms polling interval when I encountered this problem – 30 ms seemed to be working for me) where I need to rapidly send out HTTP requests just like this for a period of time (I can’t use web sockets). So, my question is why is this hang up happening and how can I fix it?

My best attempt at fixing it so far was raising the number of available file descriptors on both sides (uname -n unlimitied), but to no avail.

Testing more now… I can also reproduce the same thing with Python:

import requests

while True:
    response = requests.get("http://localhost:8000")
    print(f"Status Code: {response.status_code}")

Then python -m http.server for the server and you will get the SYN_SENT hang. So, it looks like the issue may be deeper than I originally anticipated but I’m curious and open to hearing any potential remedy.

My desired behavior is for this SYN_SENT hanging issue not to happen. I want to be able to adjust the HTTP request polling interval to be very low (even 1 ms between requests on the local network; as long as there’s no resource leakage and all requests are happening in series I don’t see why that wouldn’t be achievable) while still having my Java or other application work in a perfectly robust manner. I also want to grasp the problem, though. The view from Wireshark I find confusing because it shows that the server has returned its HTTP 200 OK response but for some reason the client hangs while reading it (I tried to include everything necessary to reproduce what I’m seeing since I’ve been debugging this for hours). Thank you for your time.

3

I’m glad to say I’ve found the root cause of this issue! I noticed that whenever I was getting these SYN_SENT hangs (as shown by ss), I was also getting this logged into my dmesg:

nf_conntrack: nf_conntrack: table full, dropping packet

The connection tracking table was getting filled up! Each HTTP 1.1 request I was making was running on its own TCP stream. It all makes perfect sense now.

I’m running a fairly unique Linux distro so I’m not sure if my connection tracking table is smaller than on average. There were also a few socket/resource leakage issues in my application which were exacerbating the issue even between separate programs on my system. I haven’t looked into increasing the size of this table, yet. If you’re running into this issue, remember that the problem could also be the small connection table of another device on the network like a router, switch, or firewall.

I left my application running overnight with a very low poll interval to see what would happen and when I woke up the Java compiler was failing because systemd had filled /tmp with these nf_conntrack logs it seems. That’s when I thought to check dmesg, and voila! Then I rebooted to clear out tmpfs.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật