You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by me...@apache.org on 2026/06/22 12:15:35 UTC

(myfaces) branch 4.0.x updated: MYFACES-4757: 4.0 Fix race conditions of CopyOnWriteList (#1024)

This is an automated email from the ASF dual-hosted git repository.

melloware pushed a commit to branch 4.0.x
in repository https://gitbox.apache.org/repos/asf/myfaces.git


The following commit(s) were added to refs/heads/4.0.x by this push:
     new 35f59edeb MYFACES-4757: 4.0 Fix race conditions of CopyOnWriteList (#1024)
35f59edeb is described below

commit 35f59edebcf77a0311e7a1872ed70cd0d7ed08ed
Author: Melloware <me...@gmail.com>
AuthorDate: Mon Jun 22 08:15:29 2026 -0400

    MYFACES-4757: 4.0 Fix race conditions of CopyOnWriteList (#1024)
---
 .../myfaces/application/ApplicationImplEventManager.java     | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/application/ApplicationImplEventManager.java b/impl/src/main/java/org/apache/myfaces/application/ApplicationImplEventManager.java
index 08c7de220..b5d905560 100644
--- a/impl/src/main/java/org/apache/myfaces/application/ApplicationImplEventManager.java
+++ b/impl/src/main/java/org/apache/myfaces/application/ApplicationImplEventManager.java
@@ -122,11 +122,11 @@ public void subscribeToEvent(Class<? extends SystemEvent> systemEventClass, Clas
         Assert.notNull(systemEventClass, "systemEventClass");
         Assert.notNull(listener, "listener");
 
+        // MYFACES-4757 use computeIfAbsent to prevent multiple threads from creating different lists
         List<EventInfo> eventInfos = globalListeners.get(systemEventClass);
         if (eventInfos == null)
         {
-            eventInfos = new CopyOnWriteArrayList<>();
-            globalListeners.put(systemEventClass, eventInfos);
+            eventInfos = globalListeners.computeIfAbsent(systemEventClass, k -> new CopyOnWriteArrayList<>());
         }
 
         EventInfo eventInfo = new EventInfo();
@@ -239,9 +239,9 @@ protected SystemEvent processComponentAttachedListeners(FacesContext facesContex
             return event;
         }
 
-        for (int i  = 0, size = listeners.size(); i < size; i++)
+        // MYFACES-4757: Use for each to prevent race condition
+        for (SystemEventListener listener : listeners)
         {
-            SystemEventListener listener = listeners.get(i);
             if (listener.isListenerForSource(source))
             {
                 // Lazy construct the event; zhis same event instance must be passed to all listener instances.
@@ -353,9 +353,9 @@ protected SystemEvent processGlobalListeners(FacesContext facesContext, List<Eve
             return event;
         }
         
-        for (int i  = 0, size = eventInfos.size(); i < size; i++)
+        // MYFACES-4757: Use for each to prevent race condition
+        for (EventInfo eventInfo : eventInfos)
         {
-            EventInfo eventInfo = eventInfos.get(i);
             if (eventInfo.sourceClass != null && !eventInfo.sourceClass.isAssignableFrom(sourceBaseType))
             {
                 continue;