TCP port for inbound agents is already in use

Article ID:115003564051
2 minute readKnowledge base

Issue

  • Agents cannot connect to Jenkins

  • controller logs show:

2017-11-27 05:47:40.670+0000 [id=57]    WARNING jenkins.model.Jenkins#launchTcpSlaveAgentListener: Failed to listen to incoming agent connections through port <PORT>. Change the port number
java.net.BindException: Address already in use
    at sun.nio.ch.Net.bind0(Native Method)
    at sun.nio.ch.Net.bind(Net.java:433)
    at sun.nio.ch.Net.bind(Net.java:425)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:67)
    at hudson.TcpSlaveAgentListener.<init>(TcpSlaveAgentListener.java:96)
Caused: java.net.BindException: Failed to listen on port <PORT> because it's already in use.
    at hudson.TcpSlaveAgentListener.<init>(TcpSlaveAgentListener.java:98)
    at jenkins.model.Jenkins.launchTcpSlaveAgentListener(Jenkins.java:1241)
    at jenkins.model.Jenkins.<init>(Jenkins.java:937)
    at hudson.model.Hudson.<init>(Hudson.java:86)
    at hudson.model.Hudson.<init>(Hudson.java:82)
    at hudson.WebAppMain$3.run(WebAppMain.java:235)
  • Agent logs show:

INFO: Locating server among [<CONTROLLER_URL>]
Nov 26, 2017 10:17:40 PM hudson.remoting.jnlp.Main$CuiListener error
SEVERE: <CONTROLLER_URL>/tcpSlaveAgentListener/ is invalid: 404 Not Found
java.io.IOException: <CONTROLLER_URL>/tcpSlaveAgentListener/ is invalid: 404 Not Found
                at org.jenkinsci.remoting.engine.JnlpAgentEndpointResolver.resolve(JnlpAgentEndpointResolver.java:167)
                at hudson.remoting.Engine.innerRun(Engine.java:499)
                at hudson.remoting.Engine.run(Engine.java:451)
  • An Administrative Monitor reads:

Failed to listen to incoming agent connections

Resolution

Most likely, you would experience this issue if another application already uses the port configured for inbound agents. In that case, Jenkins disables the port. Also, new agents cannot reconnect.

How to diagnose

Identify the port advertised by Jenkins

You can confirm whether or not the TCP port is configured for the controller by running the following:

$ curl -IL <CONTROLLER_URL>/tcpSlaveAgentListener/

If the TCP port for inbound agents is correctly configured, this command should return 200 and the header x-jenkins-jnlp-port with the value of the port.

Identify the application that listens to that port

You can check which application is listening on the port <PORT> with the following commands:

$ netstat -ntlp | grep <PORT>

or

$ lsof -i tcp -n -P | grep <PORT> | grep LISTEN

These commands help you to identify the process ID (PID) that is listening to a particular port. You can then learn more about the process with the command:

> ps -p <PID>
these commands may require to sudo

Solution

Different applications must listen on different ports in the same host. The solution is to change the Jenkins TCP port for inbound agents or change the port that the other application is using.

If you observe this issue after a restart, it most likely means that the old process did not release its resources properly. In this situation, you first need to make sure the old process is really dead. Then you can switch the port to a dummy value (say if your chosen port is 50000, choose 50001), then hit apply and switch back to the proper port. This will force Jenkins to bind again to the port. If this is not enough, then restart your instance.