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

TornadoCoreRuntime.executorThreadFactory is not thread safe #168

Open
vsilaev opened this issue Dec 24, 2021 · 0 comments
Open

TornadoCoreRuntime.executorThreadFactory is not thread safe #168

vsilaev opened this issue Dec 24, 2021 · 0 comments

Comments

@vsilaev
Copy link
Contributor

vsilaev commented Dec 24, 2021

In current code:

    private static final ThreadFactory executorThreadFactory = new ThreadFactory() {
        private int threadId = 0; // <-- a field with non-synchronized access

        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r, String.format("TornadoExecutorThread - %d", threadId));
            thread.setDaemon(true);
            threadId++;
            return thread;
        }
    };

Field threadId is used from concurrent threads (due to the nature of ThreadFactory and ExecutorService-s that using it). Hence access to this field should be either synchronized or replaced with better atomic type:

    private static final ThreadFactory executorThreadFactory = new ThreadFactory() {
        private final AtomicLong threadId = new AtomicLong(0); // <-- a field with non-synchronized access

        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r, String.format("TornadoExecutorThread - %d", threadId.incrementAndGet()));
            thread.setDaemon(true);
            return thread;
        }
    };
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

No branches or pull requests

1 participant