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

race condition in XRLog/JDKXRLogger #646

Closed
syjer opened this issue Feb 8, 2021 · 2 comments
Closed

race condition in XRLog/JDKXRLogger #646

syjer opened this issue Feb 8, 2021 · 2 comments

Comments

@syjer
Copy link
Contributor

syjer commented Feb 8, 2021

We're having what seems to be a race condition in our tests, which run in parallel in different threads :

Caused by: java.lang.NullPointerException
	at com.openhtmltopdf.util.JDKXRLogger.getLogger(JDKXRLogger.java:103)
	at com.openhtmltopdf.util.JDKXRLogger.isLogLevelEnabled(JDKXRLogger.java:75)
	at com.openhtmltopdf.util.XRLog.log(XRLog.java:122)
	at com.openhtmltopdf.util.XRLog.log(XRLog.java:85)
	at com.openhtmltopdf.context.StyleReference.getStylesheets(StyleReference.java:253)
	at com.openhtmltopdf.context.StyleReference.setDocumentContext(StyleReference.java:102)
	at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.setDocumentP(PdfBoxRenderer.java:319)
	at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.setDocumentP(PdfBoxRenderer.java:292)
	at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.<init>(PdfBoxRenderer.java:247)
	at com.openhtmltopdf.pdfboxout.PdfRendererBuilder.buildPdfRenderer(PdfRendererBuilder.java:67)
	at com.openhtmltopdf.pdfboxout.PdfRendererBuilder.run(PdfRendererBuilder.java:42)

I will put up a test case which reproduces the problem

Originally posted by @testinfected in #552 (comment)

@syjer
Copy link
Contributor Author

syjer commented Feb 8, 2021

Thanks to @testinfected 👍 .

Code for reproducing the error:

public static void main(String[] args) throws Exception {

        int p = 20;
        CountDownLatch latch = new CountDownLatch(p);
        for (int i = 0; i < p;i++) {
            new Thread(() -> {
                latch.countDown();
                try {
                    latch.await();
                    XRLog.log(Level.SEVERE, LogMessageId.LogMessageId0Param.CASCADE_IS_ABSOLUTE_CSS_UNKNOWN_GIVEN);
                } catch (InterruptedException e) {
                }
            }).start();
        }
    }

@syjer
Copy link
Contributor Author

syjer commented Feb 8, 2021

seems that moving

after the call initializeJDKLogManager() is enough to fix it. And it may make sense :).

We could even wrap the initializeJDKLogManager in a try {} block and setting the variable in a finally block.

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