From 71b7f6e0d543032b83c044556afe98d4b4a9244f Mon Sep 17 00:00:00 2001 From: metsw24-max Date: Wed, 27 May 2026 18:13:11 +0530 Subject: [PATCH 1/4] Make StringQuoter date parsing thread-safe RequestFactory and AutoBean decode Date values on the server through StringQuoter.tryParseDate, which shared two static SimpleDateFormat instances across all request threads. SimpleDateFormat parsing mutates internal Calendar state, so concurrent requests race and return wrong dates or throw. Give each thread its own formatter via ThreadLocal. --- .../autobean/shared/impl/StringQuoter.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/user/src/com/google/web/bindery/autobean/shared/impl/StringQuoter.java b/user/src/com/google/web/bindery/autobean/shared/impl/StringQuoter.java index ff4f9affbe7..09ae66b1e23 100644 --- a/user/src/com/google/web/bindery/autobean/shared/impl/StringQuoter.java +++ b/user/src/com/google/web/bindery/autobean/shared/impl/StringQuoter.java @@ -30,12 +30,20 @@ */ public class StringQuoter { private static final String ISO8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSz"; - private static final DateFormat ISO8601 = new SimpleDateFormat(ISO8601_PATTERN, Locale - .getDefault()); + private static final ThreadLocal ISO8601 = new ThreadLocal() { + @Override + protected DateFormat initialValue() { + return new SimpleDateFormat(ISO8601_PATTERN, Locale.getDefault()); + } + }; private static final String RFC2822_PATTERN = "EEE, d MMM yyyy HH:mm:ss Z"; - private static final DateFormat RFC2822 = new SimpleDateFormat(RFC2822_PATTERN, Locale - .getDefault()); + private static final ThreadLocal RFC2822 = new ThreadLocal() { + @Override + protected DateFormat initialValue() { + return new SimpleDateFormat(RFC2822_PATTERN, Locale.getDefault()); + } + }; public static Splittable create(boolean value) { return JsonSplittable.create(String.valueOf(value)); @@ -85,11 +93,11 @@ public static Date tryParseDate(String date) { date = date.substring(0, date.length() - 1) + "+0000"; } try { - return ISO8601.parse(date); + return ISO8601.get().parse(date); } catch (ParseException ignored) { } try { - return RFC2822.parse(date); + return RFC2822.get().parse(date); } catch (ParseException ignored) { } return null; From 3e9386bb9dfb6d179846fbb33246574f4e665d37 Mon Sep 17 00:00:00 2001 From: metsw24-max Date: Thu, 28 May 2026 12:42:11 +0530 Subject: [PATCH 2/4] Add StringQuoter.tryParseDate tests, including a concurrency check --- .../web/bindery/autobean/AutoBeanSuite.java | 2 + .../autobean/vm/StringQuoterJreTest.java | 115 ++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 user/test/com/google/web/bindery/autobean/vm/StringQuoterJreTest.java diff --git a/user/test/com/google/web/bindery/autobean/AutoBeanSuite.java b/user/test/com/google/web/bindery/autobean/AutoBeanSuite.java index ad47943dbc3..67b751449c0 100644 --- a/user/test/com/google/web/bindery/autobean/AutoBeanSuite.java +++ b/user/test/com/google/web/bindery/autobean/AutoBeanSuite.java @@ -22,6 +22,7 @@ import com.google.web.bindery.autobean.vm.AutoBeanCodexJreTest; import com.google.web.bindery.autobean.vm.AutoBeanJreTest; import com.google.web.bindery.autobean.vm.SplittableJreTest; +import com.google.web.bindery.autobean.vm.StringQuoterJreTest; import junit.framework.Test; @@ -38,6 +39,7 @@ public static Test suite() { suite.addTestSuite(AutoBeanTest.class); suite.addTestSuite(SplittableJreTest.class); suite.addTestSuite(SplittableTest.class); + suite.addTestSuite(StringQuoterJreTest.class); return suite; } } diff --git a/user/test/com/google/web/bindery/autobean/vm/StringQuoterJreTest.java b/user/test/com/google/web/bindery/autobean/vm/StringQuoterJreTest.java new file mode 100644 index 00000000000..d5600dbdfbd --- /dev/null +++ b/user/test/com/google/web/bindery/autobean/vm/StringQuoterJreTest.java @@ -0,0 +1,115 @@ +/* + * Copyright 2026 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.web.bindery.autobean.vm; + +import com.google.web.bindery.autobean.shared.impl.StringQuoter; + +import junit.framework.TestCase; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * Tests for the server-side {@link StringQuoter#tryParseDate(String)}. + */ +public class StringQuoterJreTest extends TestCase { + + private static final String ISO8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSz"; + private static final String RFC2822_PATTERN = "EEE, d MMM yyyy HH:mm:ss Z"; + + public void testTryParseDateMillis() { + Date d = new Date(1234567890123L); + assertEquals(d, StringQuoter.tryParseDate(Long.toString(d.getTime()))); + } + + public void testTryParseDateIso8601() { + SimpleDateFormat fmt = new SimpleDateFormat(ISO8601_PATTERN, Locale.getDefault()); + Date d = new Date(1234567890123L); + assertEquals(d, StringQuoter.tryParseDate(fmt.format(d))); + } + + public void testTryParseDateZuluSuffix() throws Exception { + SimpleDateFormat fmt = new SimpleDateFormat(ISO8601_PATTERN, Locale.getDefault()); + Date expected = fmt.parse("2024-01-15T10:30:00.000+0000"); + assertEquals(expected, StringQuoter.tryParseDate("2024-01-15T10:30:00.000Z")); + } + + public void testTryParseDateRfc2822() { + // RFC 2822 has second resolution. + SimpleDateFormat fmt = new SimpleDateFormat(RFC2822_PATTERN, Locale.getDefault()); + Date d = new Date(1234567890000L); + assertEquals(d, StringQuoter.tryParseDate(fmt.format(d))); + } + + public void testTryParseDateUnparseable() { + assertNull(StringQuoter.tryParseDate("not a date")); + } + + /** + * SimpleDateFormat is not thread-safe; sharing a single instance across + * request threads on the server produced either ParseExceptions (returned as + * null) or silently wrong Dates. Hammer tryParseDate from many threads and + * make sure every call returns the expected value. + */ + public void testTryParseDateConcurrent() throws Exception { + SimpleDateFormat fmt = new SimpleDateFormat(ISO8601_PATTERN, Locale.getDefault()); + final Date expected = new Date(1234567890123L); + final String input = fmt.format(expected); + + final int threads = 16; + final int perThread = 2000; + final CountDownLatch start = new CountDownLatch(1); + final AtomicInteger nulls = new AtomicInteger(); + final AtomicInteger wrong = new AtomicInteger(); + ExecutorService pool = Executors.newFixedThreadPool(threads); + try { + for (int i = 0; i < threads; i++) { + pool.submit(new Runnable() { + @Override + public void run() { + try { + start.await(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return; + } + for (int j = 0; j < perThread; j++) { + Date r = StringQuoter.tryParseDate(input); + if (r == null) { + nulls.incrementAndGet(); + } else if (!expected.equals(r)) { + wrong.incrementAndGet(); + } + } + } + }); + } + start.countDown(); + pool.shutdown(); + assertTrue("threads did not finish in time", pool.awaitTermination(60, TimeUnit.SECONDS)); + } finally { + pool.shutdownNow(); + } + assertEquals("parses returned null", 0, nulls.get()); + assertEquals("parses returned wrong date", 0, wrong.get()); + } +} From 9efec11e55072e3a0ceab3e2653abe161ae2b989 Mon Sep 17 00:00:00 2001 From: metsw24-max Date: Sat, 30 May 2026 11:09:47 +0530 Subject: [PATCH 3/4] Use literal inputs and check worker futures in StringQuoter tests --- .../autobean/vm/StringQuoterJreTest.java | 61 ++++++++++--------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/user/test/com/google/web/bindery/autobean/vm/StringQuoterJreTest.java b/user/test/com/google/web/bindery/autobean/vm/StringQuoterJreTest.java index d5600dbdfbd..29bc8a7d850 100644 --- a/user/test/com/google/web/bindery/autobean/vm/StringQuoterJreTest.java +++ b/user/test/com/google/web/bindery/autobean/vm/StringQuoterJreTest.java @@ -19,12 +19,13 @@ import junit.framework.TestCase; -import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; -import java.util.Locale; +import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -33,31 +34,25 @@ */ public class StringQuoterJreTest extends TestCase { - private static final String ISO8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSz"; - private static final String RFC2822_PATTERN = "EEE, d MMM yyyy HH:mm:ss Z"; + // All inputs below resolve to the same instant: 2009-02-13T23:31:30.123 UTC. + private static final Date EXPECTED = new Date(1234567890123L); public void testTryParseDateMillis() { - Date d = new Date(1234567890123L); - assertEquals(d, StringQuoter.tryParseDate(Long.toString(d.getTime()))); + assertEquals(EXPECTED, StringQuoter.tryParseDate("1234567890123")); } public void testTryParseDateIso8601() { - SimpleDateFormat fmt = new SimpleDateFormat(ISO8601_PATTERN, Locale.getDefault()); - Date d = new Date(1234567890123L); - assertEquals(d, StringQuoter.tryParseDate(fmt.format(d))); + assertEquals(EXPECTED, StringQuoter.tryParseDate("2009-02-13T23:31:30.123+0000")); } - public void testTryParseDateZuluSuffix() throws Exception { - SimpleDateFormat fmt = new SimpleDateFormat(ISO8601_PATTERN, Locale.getDefault()); - Date expected = fmt.parse("2024-01-15T10:30:00.000+0000"); - assertEquals(expected, StringQuoter.tryParseDate("2024-01-15T10:30:00.000Z")); + public void testTryParseDateZuluSuffix() { + assertEquals(EXPECTED, StringQuoter.tryParseDate("2009-02-13T23:31:30.123Z")); } public void testTryParseDateRfc2822() { // RFC 2822 has second resolution. - SimpleDateFormat fmt = new SimpleDateFormat(RFC2822_PATTERN, Locale.getDefault()); - Date d = new Date(1234567890000L); - assertEquals(d, StringQuoter.tryParseDate(fmt.format(d))); + assertEquals(new Date(1234567890000L), + StringQuoter.tryParseDate("Fri, 13 Feb 2009 23:31:30 +0000")); } public void testTryParseDateUnparseable() { @@ -67,45 +62,53 @@ public void testTryParseDateUnparseable() { /** * SimpleDateFormat is not thread-safe; sharing a single instance across * request threads on the server produced either ParseExceptions (returned as - * null) or silently wrong Dates. Hammer tryParseDate from many threads and - * make sure every call returns the expected value. + * null), other exceptions (e.g. NumberFormatException), or silently wrong + * Dates. Hammer tryParseDate from many threads at once and make sure every + * call returns the expected value and no worker threw. */ public void testTryParseDateConcurrent() throws Exception { - SimpleDateFormat fmt = new SimpleDateFormat(ISO8601_PATTERN, Locale.getDefault()); - final Date expected = new Date(1234567890123L); - final String input = fmt.format(expected); + final String input = "2009-02-13T23:31:30.123+0000"; final int threads = 16; final int perThread = 2000; + final CountDownLatch ready = new CountDownLatch(threads); final CountDownLatch start = new CountDownLatch(1); final AtomicInteger nulls = new AtomicInteger(); final AtomicInteger wrong = new AtomicInteger(); ExecutorService pool = Executors.newFixedThreadPool(threads); try { + List> futures = new ArrayList>(); for (int i = 0; i < threads; i++) { - pool.submit(new Runnable() { + futures.add(pool.submit(new Runnable() { @Override public void run() { + ready.countDown(); try { - start.await(); + if (!start.await(60, TimeUnit.SECONDS)) { + throw new AssertionError("worker was not released in time"); + } } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return; + throw new RuntimeException(e); } for (int j = 0; j < perThread; j++) { Date r = StringQuoter.tryParseDate(input); if (r == null) { nulls.incrementAndGet(); - } else if (!expected.equals(r)) { + } else if (!EXPECTED.equals(r)) { wrong.incrementAndGet(); } } } - }); + })); } + assertTrue("worker threads did not start in time", ready.await(60, TimeUnit.SECONDS)); start.countDown(); - pool.shutdown(); - assertTrue("threads did not finish in time", pool.awaitTermination(60, TimeUnit.SECONDS)); + // get() each worker so that any exception thrown inside run() (including + // an InterruptedException that would otherwise leave the work undone) is + // rethrown here and fails the test instead of being swallowed. + for (Future f : futures) { + f.get(60, TimeUnit.SECONDS); + } } finally { pool.shutdownNow(); } From dbf5570a33bbd16a8f75dcccc31bcc4d904ccd9a Mon Sep 17 00:00:00 2001 From: Sayed Kaif Date: Fri, 26 Jun 2026 00:46:55 +0530 Subject: [PATCH 4/4] Use GWT Project Authors copyright header in StringQuoterJreTest --- .../com/google/web/bindery/autobean/vm/StringQuoterJreTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user/test/com/google/web/bindery/autobean/vm/StringQuoterJreTest.java b/user/test/com/google/web/bindery/autobean/vm/StringQuoterJreTest.java index 29bc8a7d850..2102889c9bb 100644 --- a/user/test/com/google/web/bindery/autobean/vm/StringQuoterJreTest.java +++ b/user/test/com/google/web/bindery/autobean/vm/StringQuoterJreTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2026 Google Inc. + * Copyright 2026 GWT Project Authors * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of