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

fix(firmata-device): shutdown the executor when device stops #39

Merged
merged 1 commit into from
Dec 21, 2019

Conversation

kurbatov
Copy link
Owner

Fixes #38

@centic9
Copy link
Contributor

centic9 commented Dec 20, 2019

Thanks for the quick update, changes look good as far as I see.

BTW, I use something like the following in an abstract base class for all tests to verify that no unexpected threads leak anywhere:

    @Before
    public void setUp() throws InterruptedException {
        waitForThreads();
    }

    @After
    public void tearDownBase() throws InterruptedException {
        waitForThreads();
    }

    protected void waitForThreads() throws InterruptedException {
        // ensure that none of our threads are left running during shutdown
        waitForThreadToFinishSubstring("Scheduler");
        waitForThreadToFinishSubstring("qtp");
        waitForThreadToFinishSubstring("firmata");
        waitForThreadToFinishSubstring("EventThread");
    }

    private void waitForThreadToFinishSubstring(String contains) throws InterruptedException {
        waitForThreadToFinishSubstring(contains, 10_000);
        assertNoThreadLeft("Thread which contains '" + contains + "' was still running after 10 seconds", contains);
    }

    public static void assertNoThreadLeft(final String error, final String startsWith) {
        int count = Thread.currentThread().getThreadGroup().activeCount();

        Thread[] threads = new Thread[count];
        Thread.currentThread().getThreadGroup().enumerate(threads);

        for (Thread t : threads) {
            if (t != null && t.getName().startsWith(startsWith)) {
                // first take a thread-dump to report the state before we stop threads
                final String dump = new ThreadDump(true, true).toString();

                // try to stop the thread to make other tests succeed again afterwards
                AbstractPipedModule.stopThread(t.getName());

                log.severe("ThreadDump: " + dump);

                // report the problem:
                fail(error + ": " + t);
            }
        }
    }

    public static void waitForThreadToFinishSubstring(final String name, final long timeout) throws InterruptedException {
        int count = Thread.currentThread().getThreadGroup().activeCount();

        Thread[] threads = new Thread[count];
        Thread.currentThread().getThreadGroup().enumerate(threads);

        for (Thread t : threads) {
            if (t != null && t.getName().contains(name)) {
                t.join(timeout);
            }
        }
    }

@kurbatov kurbatov merged commit eb33d1f into master Dec 21, 2019
@kurbatov kurbatov deleted the fix/executor-shutdown branch December 21, 2019 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Executor is not stopped during FirmataDevice.shutdown() causing threads to leak
2 participants