Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8287580: (se) CancelledKeyException during channel registration #8978

Closed
wants to merge 5 commits into from

Conversation

bplb
Copy link
Member

@bplb bplb commented Jun 1, 2022

Ignore CancelledKeyException during registration.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8287580: (se) CancelledKeyException during channel registration

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/8978/head:pull/8978
$ git checkout pull/8978

Update a local copy of the PR:
$ git checkout pull/8978
$ git pull https://git.openjdk.org/jdk pull/8978/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 8978

View PR using the GUI difftool:
$ git pr show -t 8978

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/8978.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Jun 1, 2022

👋 Welcome back bpb! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Jun 1, 2022
@openjdk
Copy link

openjdk bot commented Jun 1, 2022

@bplb The following label will be automatically applied to this pull request:

  • nio

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the nio nio-dev@openjdk.org label Jun 1, 2022
@mlbridge
Copy link

mlbridge bot commented Jun 1, 2022

Webrevs

… SelectorImpl, open only one SocketChannel in test
@AlanBateman
Copy link
Contributor

AlanBateman commented Jun 3, 2022

Here's a suggestion for a test that is closer to what I suggested in one of the early messages. The main difference is that there is a loop calling selectNow so that the cancelled key is flushed and subsequent calls to register work as a new registration.

import java.nio.channels.*;

public class CancelDuringRegister {

    private static volatile boolean done;

    public static void main(String[] args) throws Exception {
        try (Selector sel = Selector.open()) {

            // create thread to cancel all keys in the selector's key set
            var thread = new Thread(() -> {
                while (!done) {
                    sel.keys().forEach(SelectionKey::cancel);
                }
            });
            thread.start();

            try (SocketChannel sc = SocketChannel.open()) {
                sc.configureBlocking(false);

                for (int i = 0; i <100_000; i++) {
                    
                    // register
                    var key = sc.register(sel, SelectionKey.OP_READ);
                    sel.selectNow();
                    
                    // cancel and flush
                    key.cancel();
                    do {
                        sel.selectNow();
                    } while (sel.keys().contains(key));
                }
            } finally {
                done = true;
            }

            thread.join();
        }
    }
}

@vyommani
Copy link
Contributor

vyommani commented Jun 3, 2022

It is failing with different code path as below.

$ /home/worker/source/jdk/jdk/build/linux-x86_64-server-release/images/jdk/bin/java CancelDuringRegister
Exception in thread "main" java.nio.channels.CancelledKeyException
at java.base/sun.nio.ch.SelectionKeyImpl.ensureValid(SelectionKeyImpl.java:75)
at java.base/sun.nio.ch.SelectionKeyImpl.interestOps(SelectionKeyImpl.java:104)
at java.base/java.nio.channels.spi.AbstractSelectableChannel.register(AbstractSelectableChannel.java:233)
at java.base/java.nio.channels.SelectableChannel.register(SelectableChannel.java:260)
at CancelDuringRegister.main(CancelDuringRegister.java:21)

@bplb
Copy link
Member Author

bplb commented Jun 3, 2022

It is failing with different code path as below.
[...]
at java.base/java.nio.channels.SelectableChannel.register(SelectableChannel.java:260) at CancelDuringRegister.main(CancelDuringRegister.java:21)

I see the same (linux-aarch64, windows-x64):

----------System.err:(14/895)----------
java.nio.channels.CancelledKeyException
	at java.base/sun.nio.ch.SelectionKeyImpl.ensureValid(SelectionKeyImpl.java:75)
	at java.base/sun.nio.ch.SelectionKeyImpl.interestOps(SelectionKeyImpl.java:104)
	at java.base/java.nio.channels.spi.AbstractSelectableChannel.register(AbstractSelectableChannel.java:233)
	at java.base/java.nio.channels.SelectableChannel.register(SelectableChannel.java:260)
	at CancelDuringRegister.main(CancelDuringRegister.java:51)```

@AlanBateman
Copy link
Contributor

So should we just ignore this PR for now until the test is stable?

@bplb
Copy link
Member Author

bplb commented Jun 9, 2022

So should we just ignore this PR for now until the test is stable?

Either that or go with the KeyCancelled test for now and file a separate issue to fix / replace that.

@AlanBateman
Copy link
Contributor

AlanBateman commented Jun 19, 2022

Either that or go with the KeyCancelled test for now and file a separate issue to fix / replace that.

I looked at the CancelDuringRegister test again. Two threads are racing to cancel the key. The winner queues the cancelled key to be flushed from the Selector at the next selection operation. If the winner is the background thread spinning on sel.keys().forEach(SelectionKey::cancel); then there is no guarantee that the cancelled key has been removed from the Selector's key set before the next register. So that is why it register fails intermittently with CancelledKeyException. The test can be fixed by changing it to call selectNow until the key is removed.

@bplb
Copy link
Member Author

bplb commented Jun 22, 2022

The version of CancelDuringRegister added in commit 85ebbbb failed 5/5 repeats on four platforms when run against the build jdk-19-2107 but succeeded in 5/5 repeats on four platforms when run with the suggested fix of this PR in place.

@AlanBateman
Copy link
Contributor

The latest patch has both tests, are you proposing to include both or did you mean to just include one?

@bplb
Copy link
Member Author

bplb commented Jun 22, 2022

The latest patch has both tests, are you proposing to include both or did you mean to just include one?

Just one. I simply had not removed it yet.

@openjdk
Copy link

openjdk bot commented Jun 22, 2022

@bplb This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8287580: (se) CancelledKeyException during channel registration

Reviewed-by: alanb

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 300 new commits pushed to the master branch:

  • 50c37f5: 8276798: HttpURLConnection sends invalid HTTP request
  • 270cf67: 8288752: Split thread implementation files
  • d51f4f4: 8287011: Improve container information
  • affbd72: 8288744: Remove tools/jlink/plugins/CompressorPluginTest.java from problemlist
  • 2bf5c9a: Merge
  • 70008da: 8287971: Throw exception for missing values in .jpackage.xml
  • d7b43af: 8288761: SegmentAllocator:allocate(long bytesSize) not throwing IAEx when bytesSize < 0
  • 834d92d: 8288754: GCC 12 fails to build zReferenceProcessor.cpp
  • 198cec9: 8286103: VThreadMonitorTest fails "assert(!current->cont_fastpath() || (current->cont_fastpath_thread_state() && !interpreted_native_or_deoptimized_on_stack(current))) failed"
  • 97200a7: 8278053: serviceability/jvmti/vthread/ContStackDepthTest/ContStackDepthTest.java failing in loom repo with Xcomp
  • ... and 290 more: https://git.openjdk.org/jdk/compare/239ac2a5d4c9a13e10e8c75324cd51f5f825337d...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 22, 2022
@bplb
Copy link
Member Author

bplb commented Jun 23, 2022

/integrate

@openjdk
Copy link

openjdk bot commented Jun 23, 2022

Going to push as commit 72f286a.
Since your change was applied there have been 315 commits pushed to the master branch:

  • b8db0c3: 6980847: (fs) Files.copy needs to be "tuned"
  • d579916: 8288740: Change incorrect documentation for sjavac flag
  • 26c03c1: 8288719: [arm32] SafeFetch32 thumb interleaving causes random crashes
  • a802b98: 8287760: --do-not-resolve-by-default gets overwritten if --warn-if-resolved flags is used
  • bf0623b: 8286314: Trampoline not created for far runtime targets outside small CodeCache
  • 5b583e4: Merge
  • 6458ebc: 8288988: ProblemList serviceability/jvmti/vthread/ContStackDepthTest/ContStackDepthTest.java in -Xcomp mode
  • 6037ccd: 8288846: misc tests fail "assert(ms < 1000) failed: Un-interruptable sleep, short time use only"
  • 8fa46c8: 8288840: StructureViolationException should not link to fork method
  • 7cf71bc: 8287982: Concurrent implicit attach from native threads crashes VM
  • ... and 305 more: https://git.openjdk.org/jdk/compare/239ac2a5d4c9a13e10e8c75324cd51f5f825337d...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jun 23, 2022
@openjdk openjdk bot closed this Jun 23, 2022
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jun 23, 2022
@openjdk
Copy link

openjdk bot commented Jun 23, 2022

@bplb Pushed as commit 72f286a.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@bplb bplb deleted the CancelledKeyException-8287580 branch June 23, 2022 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrated Pull request has been integrated nio nio-dev@openjdk.org
3 participants