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

bond java possible deadlock when create class and inner class at the same time #1022

Open
Zhuoqing opened this issue Jan 27, 2020 · 0 comments
Labels

Comments

@Zhuoqing
Copy link

Zhuoqing commented Jan 27, 2020

This is probably by design. StructBondType is sharing global lock across all bond class, while JVM will take Class lock for clinit class. So when ClassOut and ClassIn are being created in different thread, deadlock could possible happen when
ClassOut clinit take the StructBondType lock, trying to create ClassIn and wait for ClassIn clinit lock,

while ClassIn take the clinit lock, while wait for StructBondType lock

The workaround we did is simply pre-init those classes.

Bond file

namespace TestBondClassLock

struct ClassIn
{
    1: int32 InField;
}

struct ClassOut
{
	1: ClassIn InClass;
}

Test program

import TestBondClassLock.ClassIn;
import TestBondClassLock.ClassOut;

public class Main {

    public static void main(String[] args) {

        System.out.println("Creating Thread...");
        Thread thread1 = new Thread(() -> {
            System.out.println("outClass creating : " + Thread.currentThread().getName());
            ClassOut classOut = new ClassOut();
            System.out.println("outClass created : " + classOut.toString() + Thread.currentThread().getName());
        }, "ClassOutCreateThread");
        Thread thread2 = new Thread(() -> {
            System.out.println("ClassIn creating : " + Thread.currentThread().getName());
            ClassIn classIn = new ClassIn();
            System.out.println("ClassIn created : " + classIn.toString() + Thread.currentThread().getName());
        }, "ClassInCreateThread");

        thread1.start();
        thread2.start();
        try {
            thread1.join();
            thread2.join();
        }
        catch (InterruptedException ie) {
            System.out.println("InterruptedException:" + ie.getMessage() + ie.getStackTrace());
        }
        System.out.println("Finish!");
    }
}

@chwarr chwarr added the bug label Feb 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants