flow icon indicating copy to clipboard operation
flow copied to clipboard

I18NProvider failed to load resource bundle when called from non-UI thread

Open ijioio opened this issue 11 months ago • 0 comments

Description of the bug

When running application in production mode (fat spring boot jar build by spring gradle plugin) I18NProvider unable to load resource bundle and gracefully failed with the following warning:

2024-03-15T15:11:25.018+03:00  WARN 5804 --- [onPool-worker-1] c.vaadin.flow.i18n.DefaultI18NProvider   : Missing resource bundle for vaadin-i18n.translations and locale  яюэёъшщ

java.util.MissingResourceException: Can't find bundle for base name vaadin-i18n.translations, locale ja
        at java.base/java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:2045) ~[na:na]
        at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1683) ~[na:na]
        at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1586) ~[na:na]
        at java.base/java.util.ResourceBundle.getBundle(ResourceBundle.java:1280) ~[na:na]
        at com.vaadin.flow.i18n.DefaultI18NProvider.getBundle(DefaultI18NProvider.java:87) ~[flow-server-24.3.7.jar!/:24.3.7]
        at com.vaadin.flow.i18n.DefaultI18NProvider.getTranslation(DefaultI18NProvider.java:67) ~[flow-server-24.3.7.jar!/:24.3.7]
        at com.vaadin.flow.component.Component.lambda$getTranslation$3(Component.java:641) ~[flow-server-24.3.7.jar!/:24.3.7]
        at java.base/java.util.Optional.map(Optional.java:260) ~[na:na]
        at com.vaadin.flow.component.Component.getTranslation(Component.java:641) ~[flow-server-24.3.7.jar!/:24.3.7]
        at i18nbug.MainView.lambda$beforeEnter$c534042c$1(MainView.java:62) ~[!/:na]
        at com.vaadin.flow.component.UI.accessSynchronously(UI.java:489) ~[flow-server-24.3.7.jar!/:24.3.7]
        at com.vaadin.flow.component.UI$2.execute(UI.java:564) ~[flow-server-24.3.7.jar!/:24.3.7]
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[na:na]
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
        at com.vaadin.flow.server.VaadinService.runPendingAccessTasks(VaadinService.java:2082) ~[flow-server-24.3.7.jar!/:24.3.7]
        at com.vaadin.flow.server.VaadinSession.unlock(VaadinSession.java:708) ~[flow-server-24.3.7.jar!/:24.3.7]
        at com.vaadin.flow.server.VaadinService.ensureAccessQueuePurged(VaadinService.java:2047) ~[flow-server-24.3.7.jar!/:24.3.7]
        at com.vaadin.flow.server.VaadinService.accessSession(VaadinService.java:2014) ~[flow-server-24.3.7.jar!/:24.3.7]
        at com.vaadin.flow.server.VaadinSession.access(VaadinSession.java:1012) ~[flow-server-24.3.7.jar!/:24.3.7]
        at com.vaadin.flow.component.UI.access(UI.java:561) ~[flow-server-24.3.7.jar!/:24.3.7]
        at com.vaadin.flow.component.UI.access(UI.java:544) ~[flow-server-24.3.7.jar!/:24.3.7]
        at i18nbug.MainView.lambda$beforeEnter$0(MainView.java:61) ~[!/:na]
        at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1804) ~[na:na]
        at java.base/java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1796) ~[na:na]
        at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373) ~[na:na]
        at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182) ~[na:na]
        at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655) ~[na:na]
        at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622) ~[na:na]
        at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165) ~[na:na]

All translations resources are included in the jar as expected. Moreover part of the components on a static part of the page are translated OK. It seems to be happened only during async call, when updating component (for example, Grid) from non-UI thread within ui.access() block. After all data is loaded and async call is finished, switching locale on grid from the UI thread applies translations without a problem.

Note, that it occurs only if running in production mode. Running it in IDE (Eclipse) using development mode is OK.

Workaround

It appears that DefaultI18NProvider trying to load resource bundle using java.util.ResourceBundle.getBundle(String, Locale, ClassLoader):

    private ResourceBundle getBundle(Locale locale) {
        try {
            return ResourceBundle.getBundle(BUNDLE_PREFIX, locale,
                    I18NUtil.getClassLoader());
        } catch (final MissingResourceException e) {
            getLogger().warn("Missing resource bundle for " + BUNDLE_PREFIX
                    + " and locale " + locale.getDisplayName(), e);
        }
        return null;
    }

Using custom I18NProvider that loads resource bundle via java.util.ResourceBundle.getBundle(String, Locale) do the trick

Expected behavior

DefaultI18NProvider should not fail trying to load resource bundle when called from non-UI thread

Minimal reproducible example

i18nbug.zip

  1. Download and extract i18nbug.zip
  2. Go to i18nbug directory
  3. Run gradlew vaadinclear
  4. Run gradlew vaadinbuildfrontend
  5. Run gradlew assemble
  6. Run application java -jar lib/build/libs/i18nbug.jar
  7. Access in a web browser http://localhost:8080

First two spans will be displayed having translated values Span1 and Span2. At the same time an async task will be executed emulating some long task (1000 ms) and after finish it will try to update value of second span with Span3 value from ui.access() block but will fail

Versions

  • Vaadin / Flow version: 24.3.7
  • Java version: 17
  • OS version: Win11
  • Browser version (if applicable):
  • Application Server (if applicable):
  • IDE (if applicable):

ijioio avatar Mar 15 '24 13:03 ijioio