<AWT Dev> RFR: 8262731: [macOS] Exception from "Printable.print" is swallowed during "PrinterJob.print"
alitvinov at openjdk.java.net
Fri May 21 19:14:20 UTC 2021
On Wed, 19 May 2021 15:22:27 GMT, Phil Race <prr at openjdk.org> wrote:
>> Could you please review the following fix for the bug specific to macOS. The bug consists in the fact that if the method "java.awt.print.Printable.print(Graphics, PageFormat, int)" throws "java.awt.print.PrinterException" or "java.lang.RuntimeException" during the call "java.awt.print.PrinterJob.print()", then the exception is caught and ignored by JDK and a user cannot learn that printing failed and what caused failure of printing, because "PrinterJob.print()" method does not throw "PrinterException" or the occurred exception is not reported by JDK through the error stream.
>> ROOT CAUSE OF THE BUG:
>> The root cause of the bug is the fact that in the method "sun.lwawt.macosx.CPrinterJob.printAndGetPageFormatArea(final Printable, final Graphics, final PageFormat, final int)" from the file "src/java.desktop/macosx/classes/sun/lwawt/macosx/CPrinterJob.java" the exception thrown during execution of the expression
>> "int pageResult = printable.print(graphics, pageFormat, pageIndex);"
>> is caught but is not returned to a developer by any mean or is not printed out to the error stream.
>> THE FIX:
>> The fix implements propagation of the occurred and caught exception to the level of the user's code executing "PrinterJob.print()" method. Propagation of the exception by storing it in the instance variable of "CPrinterJob" object is implemented, because the engaged code always is executed:
>> - on 2 threads (non-EDT thread, EDT thread) in case when "PrinterJob.print()" is called by the user on a non-EDT thread;
>> - on 3 threads (2 EDT threads, a temporary thread started by JDK to execute "CPrinterJob._safePrintLoop(long, long );") when "PrinterJob.print()" is called on EDT thread.
>> The regression test which is part of the fix was also successfully executed on MS Windows OS and Linux OS.
>> Thank you,
> test/jdk/java/awt/print/PrinterJob/ExceptionFromPrintableIsIgnoredTest.java line 83:
>> 81: return NO_SUCH_PAGE;
>> 82: }
>> 83: throw new PrinterException("Exception from Printable.print");
> Don't you want to also test this with some kind of RuntimeException ?
In the 1st version of the fix I did not test the scenario with "RuntimeException", because despite of the fact that I had known that JDK 17 will pass on Windows and Linux the scenario with "RuntimeException" I still was not sure if the behavior of older JDK release families is the same with JDK 17 on Windows and Linux, and just did not want to run the risk of failure of this test during potential porting of the fix to older JDK release families.
But yesterday I did testing with my standalone test case attached to the bug record in JBS and now I can certainly say that JDK 8u291-b10, JDK JDK 8-b132 do not fail in the test scenarios with "RuntimeException" on Windows and Linux. Therefore it is safe to include test scenario with "RuntimeException" to the regression test.
In the 2nd version of the fix now I am testing also "RuntimeException" both on EDT thread and on main thread.
More information about the awt-dev