From 1295fb19329025e242da556223b1cf935feef2b6 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Tue, 18 Oct 2016 20:12:54 +0200 Subject: [PATCH] Improve the caller/callee correlation diagnostics I was trying to diagnose a remoting call that bridges two VMs that is hanging. This can be made easier if thread dump allows us to correlate the caller/callee. This change adds the request ID to thread name so that thread dump will show this clearly Also fixes one incorrect use of toString() in diagnosis, which should be getName() --- see all the other uses of thread names. --- src/main/java/hudson/remoting/Request.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/hudson/remoting/Request.java b/src/main/java/hudson/remoting/Request.java index 2839b546a..ab6f62958 100644 --- a/src/main/java/hudson/remoting/Request.java +++ b/src/main/java/hudson/remoting/Request.java @@ -139,7 +139,7 @@ public final RSP call(Channel channel) throws EXC, InterruptedException, IOExcep final String name = t.getName(); try { // wait until the response arrives - t.setName(name+" / waiting for "+channel); + t.setName(name+" / waiting for "+channel.getName()+" id="+id); while(response==null && !channel.isInClosed()) // I don't know exactly when this can happen, as pendingCalls are cleaned up by Channel, // but in production I've observed that in rare occasion it can block forever, even after a channel @@ -233,6 +233,8 @@ public boolean isDone() { public RSP get() throws InterruptedException, ExecutionException { synchronized(Request.this) { + String oldThreadName = Thread.currentThread().getName(); + Thread.currentThread().setName(oldThreadName+" for "+channel.getName()+" id="+id); try { while(response==null) { if (isCancelled()) { @@ -250,6 +252,8 @@ public RSP get() throws InterruptedException, ExecutionException { // couldn't cancel. ignore. } throw e; + } finally { + Thread.currentThread().setName(oldThreadName); } if(response.exception!=null) @@ -320,7 +324,7 @@ private int calcLastIoId() { public void run() { String oldThreadName = Thread.currentThread().getName(); - Thread.currentThread().setName(oldThreadName+" for "+channel.getName()); + Thread.currentThread().setName(oldThreadName+" for "+channel.getName()+" id="+id); try { Command rsp; CURRENT.set(Request.this);