GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: fix interrupt spiral in grpc ReadObject drainQueue by BenWhitehead · Pull Request #2850 · googleapis/java-storage

This repository was archived by the owner on Sep 25, 2026. It is now read-only.

Conversation

@BenWhitehead

@BenWhitehead BenWhitehead commented Dec 16, 2024

Copy link
Copy Markdown
Collaborator

If our thread is interrupted while attempting to drainQueue poll will throw an InterruptedException, instead of setting the flag back on the thread immediately we need to defer setting it until we complete our draining. If we don't defer setting it, we can never actually drain our queue.

Manually tested repro and fix with the following code sample, which mirrors the pattern in drainQueue.

import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.concurrent.ArrayBlockingQueue;

final class InterruptSpiral {

  public static void main(String[] args) {
    final ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(2);

    Thread main = Thread.currentThread();
    Thread thread = new Thread(() -> {
      try {
        queue.add("String 1");
        main.interrupt();
        Thread.sleep(2500);
        queue.add("String 2");
      } catch (InterruptedException ignore) {}
    });

    IOException ioException = null;
    boolean shouldInterupt = false;
    thread.start();
    int taken = 0;
    for (int i = 0; i < 5; i++) {
      System.out.println("while");
      System.out.println("main.isInterrupted() = " + main.isInterrupted());
      try {
        System.out.println("pre  = " + System.nanoTime());
        String take = queue.take();
        taken++;
        System.out.println("post = " + System.nanoTime());
        System.out.println("take = " + take);
        if (taken == 2) {
          break;
        }
      } catch (InterruptedException e) {
        // Thread.currentThread().interrupt(); <-- the bug
        shouldInterupt = true;
        if (ioException == null) {
          ioException = new InterruptedIOException();
        } else {
          ioException.addSuppressed(e);
        }
      }
    }
    if (shouldInterupt) {
      Thread.currentThread().interrupt();
    }
    System.out.println("main.isInterrupted() = " + main.isInterrupted());
    thread.interrupt();
    ioException.printStackTrace();
  }
}

Without the fix, prints the following:

while
main.isInterrupted() = false
pre  = 1229541299752426
post = 1229541300220917
take = String 1
while
main.isInterrupted() = true
pre  = 1229541300328206
while
main.isInterrupted() = true
pre  = 1229541300435077
while
main.isInterrupted() = true
pre  = 1229541300565877
while
main.isInterrupted() = true
pre  = 1229541300633267
main.isInterrupted() = true
java.io.InterruptedIOException
	at com.google.cloud.storage.InterruptSpiral.main(InterruptSpiral.java:61)
	Suppressed: java.lang.InterruptedException
		at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1220)
		at java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:335)
		at java.util.concurrent.ArrayBlockingQueue.take(ArrayBlockingQueue.java:400)
		at com.google.cloud.storage.InterruptSpiral.main(InterruptSpiral.java:50)
	Suppressed: java.lang.InterruptedException
		at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1220)
		at java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:335)
		at java.util.concurrent.ArrayBlockingQueue.take(ArrayBlockingQueue.java:400)
		at com.google.cloud.storage.InterruptSpiral.main(InterruptSpiral.java:50)
	Suppressed: java.lang.InterruptedException
		at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1220)
		at java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:335)
		at java.util.concurrent.ArrayBlockingQueue.take(ArrayBlockingQueue.java:400)
		at com.google.cloud.storage.InterruptSpiral.main(InterruptSpiral.java:50)

With the fix, prints the following:

while
main.isInterrupted() = false
pre  = 1229196113678731
post = 1229196114515921
take = String 1
while
main.isInterrupted() = true
pre  = 1229196114614861
while
main.isInterrupted() = false
pre  = 1229196114718311
post = 1229198614527216
take = String 2
main.isInterrupted() = true
java.io.InterruptedIOException
	at com.google.cloud.storage.InterruptSpiral.main(InterruptSpiral.java:71)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jterrace reacted with hooray emoji
All reactions
  • 🎉 1 reaction
@BenWhitehead
BenWhitehead requested a review from a team December 16, 2024 23:25
@product-auto-label product-auto-label Bot added size: m Pull request size is medium. api: storage Issues related to the googleapis/java-storage API. labels Dec 16, 2024
@BenWhitehead
fix: fix interrupt spiral in grpc ReadObject drainQueue
27e6aef
If our thread is interrupted while attempting to drainQueue poll will throw an InterruptedException, instead of setting the flag back on the thread immediately we need to defer setting it until we complete our draining. If we don't defer setting it, we can never actually drain our queue.
@BenWhitehead
BenWhitehead force-pushed the fix-interrupt-bomb branch from babf3ae to 27e6aef Compare December 16, 2024 23:33
sydney-munro
sydney-munro reviewed Dec 16, 2024
View reviewed changes

@sydney-munro sydney-munro left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, 1 question before approval.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...cloud-storage/src/main/java/com/google/cloud/storage/GapicUnbufferedReadableByteChannel.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

sydney-munro
sydney-munro approved these changes Dec 16, 2024
View reviewed changes
danielduhh
danielduhh reviewed Dec 16, 2024
View reviewed changes
Comment thread ...cloud-storage/src/main/java/com/google/cloud/storage/GapicUnbufferedReadableByteChannel.java
@@ -234,35 +234,42 @@ public void close() throws IOException {
}

private void drainQueue() throws IOException {

@danielduhh danielduhh Dec 16, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, we added this queuing code as a response to b/364531464. Can you please clarify what was missed or why the previous implementation didn't work as expected? Just want to better understand the cause/intended fix/new fix.

Also, Can we add some tests?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@BenWhitehead BenWhitehead Dec 17, 2024

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sentences before the code sample in the description outline why this is necessary and was was missed the first time around. The code attached to this PR simulates the scenario which caused the interrupt spiral, unfortunately there isn't a reliable way for us to force the interrupt to happen during drainQueue while the loop is processing. It's a multithreaded race scenario.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@danielduhh danielduhh Dec 17, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some extra context for posterity from chatting with Ben + Syd: The code here was created to replace gax and add some extra memory management features.
The actual bug is in the while loop that causes the interruption to catch its own exception indefinitely (hence moving the InterruptedException inside the loop)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@BenWhitehead
BenWhitehead merged commit c1dac83 into main Dec 17, 2024
@BenWhitehead
BenWhitehead deleted the fix-interrupt-bomb branch December 17, 2024 20:30
@release-please release-please Bot mentioned this pull request Dec 17, 2024
chore(main): release 2.47.0 #2854
Merged
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

2 more reviewers
@danielduhh danielduhh danielduhh left review comments
@sydney-munro sydney-munro sydney-munro approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

api: storage Issues related to the googleapis/java-storage API. size: m Pull request size is medium.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@BenWhitehead @danielduhh @sydney-munro
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.