Tomcat fails to remove ThreadLocal on shutdown/undeploy when setting default font

Hello,

we are converting html to pdf using java, and the conversion code is deployed on the Tomcat as part of a web application.
The problem we are experiencing is that when the Tomcat is shut down, the following is written to the log:

06-Oct-2021 16:16:17.957 SEVERE [Thread-4] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mywebapp] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@33bfe508]) and a value of type [com.aspose.pdf.internal.imaging.system.Threading.Thread] (value [com.aspose.pdf.internal.imaging.system.Threading.Thread@51900969]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.

Actually, I had had created a thread on the topic Tomcat fails to remove ThreadLocal on undeploy/shutdown The problem was marked as fixed and it could not be reproduced with my previous sample application. But it still was present in our more complex application, and I tried to determine what else might be causing exceptions on shutdown. I found out that if the following line is added to the servlet code, exceptions start occurring:

	FontSettings.setDefaultFontName("Arial");

Is there a way to get rid of the exception?

The relevant piece of code used to reproduce it:

private byte[] getDocument()
{
	FontSettings.setDefaultFontName("Arial");

	byte[] result = null;
	String htmlString = "<html><head><title>Title</title></head><body><p>Body</p></body></html>";
	InputStream inputStream = new ByteArrayInputStream(htmlString.getBytes());
	HtmlLoadOptions options = new HtmlLoadOptions();
	options.setInputEncoding(StandardCharsets.UTF_8.name());
	Document document = new Document(inputStream, options);
	try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
	{
		document.save(outputStream);
		result = outputStream.toByteArray();

	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
	finally
	{
		document.close();
		document.dispose();
		MemoryCleaner.clear();
		MemoryCleaner.clearCurrentThreadLocals();
	}
	return result;
}

Aspose version: 21.8
Java version: 16
Tomcat version: 9.0.43

Thank you

Arjana Bivainiene

@arjana

We need to further investigate the scenario. Can you please share a sample source HTML file for our reference so that we can further proceed accordingly?

I put a very simple html directly into the code as the exception does not seem to be related to its content:

<html><head><title>Title</title></head><body><p>Body</p></body></html>

I also uploaded the project files to dropbox (only licence is removed from the project): https://www.dropbox.com/s/dqglyu002b5xz91/AsposeWebApp.zip?dl=0

To reproduce the exception, conversion needs to be invoked before shutdown: http://host:port/converter/api/createpdf

@arjana

We have logged another issue as PDFJAVA-40913 in our issue management system for this case. We will look into its details and let you know as soon as the ticket is resolved. Please be patient and spare us some time.

We are sorry for the inconvenience.