[ 
https://issues.apache.org/jira/browse/PDFBOX-3521?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15543363#comment-15543363
 ] 

rob hinds commented on PDFBOX-3521:
-----------------------------------

The investigation I did was only fairly preliminary, so I may be wrong with my 
assertions - but the behaviour I saw, was during some unit tests (run by 
gradle) I was getting a NullPointerException whilst trying to acces 
fontInfoByName:
{code}
WARNING: Using fallback font 'LiberationSans-Bold' for 'Arial-BoldItalicMT'
  java.lang.NullPointerException:
  at 
org.apache.pdfbox.pdmodel.font.FontMapperImpl.getFont(FontMapperImpl.java:463)
  at 
org.apache.pdfbox.pdmodel.font.FontMapperImpl.findFont(FontMapperImpl.java:417)
  at 
org.apache.pdfbox.pdmodel.font.FontMapperImpl.getTrueTypeFont(FontMapperImpl.java:321)
  at 
org.apache.pdfbox.pdmodel.font.PDTrueTypeFont.<init>(PDTrueTypeFont.java:198)
  at 
org.apache.pdfbox.pdmodel.font.PDFontFactory.createFont(PDFontFactory.java:75)
  at org.apache.pdfbox.pdmodel.PDResources.getFont(PDResources.java:123)
  at 
org.apache.pdfbox.contentstream.operator.text.SetFontAndSize.process(SetFontAndSize.java:60)
  at 
org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:815)
  at 
org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:472)
  at 
org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:446)
  ...
Oct 03, 2016 12:21:24 PM org.apache.pdfbox.pdmodel.font.PDTrueTypeFont <init>
WARNING: Using fallback font 'LiberationSans-Bold' for 'Arial-BoldMT'
Oct 03, 2016 12:21:24 PM org.apache.pdfbox.pdmodel.font.PDTrueTypeFont <init>
{code}

So it looks like because the checks are for fontProvider are used to determine 
if the initialisation has been completed, but that can be initialised whilst 
fontByInfoName is still not initialised (if this is the case, it seems like it 
could be fixed by changing the order of the setProvider method call so that the 
fontProvider is initialised last - being as that is what is being used to check 
if initialisation complete - but there is probably a prettier solution).

> 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: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to