Skip to content

Commit

Permalink
Adjust the clipboard retry logic to use exponential backoff. It was c…
Browse files Browse the repository at this point in the history
…reating a lot of logging messages, and clipboard access is a heavy operation that can sometimes interfere with other applications.
  • Loading branch information
eirikbakke committed Sep 27, 2024
1 parent 8bb527a commit 534340c
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions platform/o.n.bootstrap/src/org/netbeans/NbClipboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,20 +424,25 @@ public void run() {
// that is used because accessing the clipboard can block
// indefinitely. Running the access loop here is deemed similar
// in nature.
final int MAX_TRIES = 50;

/* The loop will actually stop before getting to 10 iterations, per the delay
formula and conditional throw. But keep the MAX_TRIES just as a fail-safe. */
final int MAX_TRIES = 10;
final long start = System.currentTimeMillis();
int delay = 20;
for (int i = 0; i < MAX_TRIES; i++) {
try {
transferable = systemClipboard.getContents(this);
break;
} catch (IllegalStateException ex) {
// Throw exception if retries failed
if (i == (MAX_TRIES - 1) || (System.currentTimeMillis() - start) > 980L) {
if (i == (MAX_TRIES - 1) || (System.currentTimeMillis() + delay - start) > 1000L) {
throw ex;
} else {
log.log(Level.INFO, "systemClipboard#getContents threw IllegalStateException (try: {0})", i + 1); // NOI18N
log.log(Level.INFO, "systemClipboard#getContents ISE, attempt {0}", i + 1); // NOI18N
}
Thread.sleep(20); // Give system time to settle
Thread.sleep(delay); // Give system time to settle
delay *= 2;
}
}
superSetContents(transferable, null);
Expand Down

0 comments on commit 534340c

Please sign in to comment.