+

+
+
 +
If you have agents that connect through https://wiki.jenkins.io/display/JENKINS/Distributed+builds#Distributedbuilds-LaunchslaveagentviaJavaWebStart[Java Web Start] (such as https://wiki.jenkins.io/display/JENKINS/Installing+Jenkins+as+a+Windows+service#InstallingJenkinsasaWindowsservice-InstallSlaveasaWindowsservice%28require.NET2.0framework%29[agents installed as Windows services]), we have a good news for you. +
 +
 +
In case of a connection loss, this type of agents has been designed to automatically attempt to reconnect to the controller. This makes sense because you want these agents to remain online all the time, even if your janitor trips over the ethernet cable. Unfortunately, it also means that over the time, these agents accumulate gunk, such as mutated static states, any left-over threads or memory leaks, or https://issues.jenkins.io/browse/JENKINS-20913[native libraries that are loaded into JVM]. +
 +
 +
To prevent that, a better approach is to https://issues.jenkins.io/browse/JENKINS-19055[restart the agent JVM (JENKINS-19055)] and have the new JVM reconnect, instead of having the same JVM reconnect. That would ensure that the agent always stays clean. I've planned to make this change for a while now, and I'm happy to report that this change is finally landing to the upcoming 1.559. +
 +
 +
Restarting JVM is easy on Unix, where I could just https://man7.org/linux/man-pages/man3/exec.3.html[exec(3)] to itself. We've been doing this for ages on controllers, for example when you update a plugin and tell Jenkins to restart. +
 +
 +
The hard part is to do this for Windows, where the most of the time was spent. I had to improve https://github.com/kohsuke/winsw[windows service wrapper] to support self-restarting services, which turned out to be trickier because Windows service control manager doesn't provide "restart" as an atomic operation. It also kills not just the service process itself but all the processes in the group. So I had to double-fork the service wrapper into a separate process group just to restart a service from within itself. +
 +
 +
In any case, the end result is that if https://wiki.jenkins.io/display/JENKINS/Installing+Jenkins+as+a+Windows+service#InstallingJenkinsasaWindowsservice-InstallSlaveasaWindowsservice%28require.NET2.0framework%29[you have installed a service through GUI], be it on Windows, Unix, or OS X, agents will restart themselves every time it gets disconnected from the controller. +
 +
 +
I've also taken the opportunity to make `+jenkins-slave.exe+` on the agent self-updating. Every time it connects to the controller, it gets the latest version from the controller. +
 +
 +
If you have installed Web Start agents as services, make sure to update the local copy of `+agent.jar+` on these agents to 2.37 or later. This "restart on reconnect" feature only kicks in when you are running this very recent version of `+agent.jar+`. And yes, we realize it'd be nice for `+agent.jar+` to update itself, which is tracked as https://issues.jenkins.io/browse/JENKINS-22454[JENKINS-22454]. But that's a work for another day. +
 +
About the Author
Kohsuke Kawaguchi

Kohsuke is the creator of Jenkins.