You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "rob hinds (JIRA)" <ji...@apache.org> on 2016/10/03 13:34:20 UTC

[jira] [Created] (PDFBOX-3521) FontProvider not thread safe

rob hinds created PDFBOX-3521:
---------------------------------

             Summary: FontProvider not thread safe
                 Key: PDFBOX-3521
                 URL: https://issues.apache.org/jira/browse/PDFBOX-3521
             Project: PDFBox
          Issue Type: Bug
            Reporter: rob hinds
            Priority: Minor


I understand that PDFBox is not yet thread-safe, however, from reading this comment on a [similar ticket|https://issues.apache.org/jira/browse/PDFBOX-3071?focusedCommentId=14982069&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-14982069], it seems like the fonts should be thread safe and is something that we want to fix. However, I am experiencing an intermittent  NullPointerException when running multi-threaded unit tests that attempt to read several PDFs at once (see details [here|http://stackoverflow.com/questions/39831376/pdfbox-npe-loading-fonts].

Inside FontMapperImpl.java the getProvider and setProvider methods are synchronised:

{code}
    public synchronized void setProvider(FontProvider fontProvider)
    {
        this.fontProvider = fontProvider;
        fontInfoByName = createFontInfoByName(fontProvider.getFontInfo());
    }

    /**
     * Returns the font service provider. Defaults to using FileSystemFontProvider.
     */
    public synchronized FontProvider getProvider()
    {
        if (fontProvider == null)
        {
            setProvider(DefaultFontProvider.INSTANCE);
        }
        return fontProvider;
    }
{code}

However, the calling code from the findFont() method is not synchronised:
{code}
        // make sure the font provider is initialized
        if (fontProvider == null)
        {
            getProvider();
        }

        // first try to match the PostScript name
        FontInfo info = getFont(format, postScriptName);
        if (info != null)
        {
            return info.getFont();
        }
{code}

As a result, if multiple threads attempt to access this at once, thread A may be in the setProvider method and have set fontProvider, but still processing the creation of fontInfoByName - so thread B could attempt to access before initialised.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: dev-help@pdfbox.apache.org