Skip to contentSkip to conversation

Fix: Unsynchronized access in LTTngTCPSessiondClient

PATCH New Mathieu Desnoyers 2 participants 7 messages Feb 22, 2014 – Feb 25, 2014 5 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

----- Original Message -----

Quoted 41 lines

From: "Jérémie Galarneau" <[email protected]>
To: [email protected]
Sent: Thursday, February 20, 2014 11:22:33 AM
Subject: [lttng-dev] [PATCH lttng-ust] Fix: Unsynchronized access in LTTngTCPSessiondClient

enabledEventList is shared between the LTTngThread and eventTimer threads but is not synchronized.

This patch changes enabledEventList's type from an ArrayList to a synchronizedList which implements the same interface.

---
 .../org/lttng/ust/jul/LTTngTCPSessiondClient.java         | 15
 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +3 -1
index 25d1cb2..0d9a485 100644
@@ -34,6 +34,7 @@ import java.util.List;
 import java.util.Timer;
 import java.util.TimerTask;
 import java.util.logging.Logger;
+import java.util.Collections;
 
 class USTRegisterMsg {
 	public static int pid;
@@ -57,7 +58,8 @@ public class LTTngTCPSessiondClient {
 	private Semaphore registerSem;
 
 	private Timer eventTimer;
-	private List<LTTngEvent> enabledEventList = new ArrayList<LTTngEvent>();
+	private List<LTTngEvent> enabledEventList = Collections.synchronizedList(
+		new ArrayList<LTTngEvent>());
 	/*
 	 * Map of Logger objects that have been enabled. They are indexed by name.
 	 */

liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java public class LTTngTCPSessiondClient { · line 94

Quoted 8 lines
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +4 -1
@@ -86,7 +88,10 @@ public class LTTngTCPSessiondClient {
 				 * We have to make a copy here since it is possible that the
 				 * enabled event list is changed during an iteration on it.
 				 */
-				List<LTTngEvent> tmpList = new ArrayList<LTTngEvent>(enabledEventList);
+				List<LTTngEvent> tmpList;
+				synchronized(enabledEventList) {
+					tmpList = new ArrayList<LTTngEvent>(enabledEventList);
+				}

Further down in this function, we find:

                                        ret = enableCmd.enableLogger(handler, event, enabledLoggers);
                                        if (ret == 1) {
                                                /* Enabled so remove the event from the list. */
                                                enabledEventList.remove(event);
                                        }

should we encompass a larger part of this function with synchronize ?

What happens if event is removed from enabledEventList while we iterate on the list copy ?

Thanks,

MAthieu

Quoted 25 lines
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +4 -3
 				LTTngSessiondCmd2_4.sessiond_enable_handler enableCmd = new
 					LTTngSessiondCmd2_4.sessiond_enable_handler();
@@ -277,8 +282,10 @@ public class LTTngTCPSessiondClient {
 						 * Add the event to the list so it can be enabled if
 						 * the logger appears at some point in time.
 						 */
-						if (enabledEventList.contains(event) == false) {
-							enabledEventList.add(event);
+						synchronized (enabledEventList) {
+							if (enabledEventList.contains(event) == false) {
+								enabledEventList.add(event);
+							}
 						}
 					}
 					data = enableCmd.getBytes();
--

1.9.0

_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

Signature · 8 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 54 lines · Mathieu Desnoyers

----- Original Message -----

Quoted 41 lines

From: "Jérémie Galarneau" <[email protected]>
To: [email protected]
Sent: Thursday, February 20, 2014 11:22:33 AM
Subject: [lttng-dev] [PATCH lttng-ust] Fix: Unsynchronized access in LTTngTCPSessiondClient

enabledEventList is shared between the LTTngThread and eventTimer threads but is not synchronized.

This patch changes enabledEventList's type from an ArrayList to a synchronizedList which implements the same interface.

---
 .../org/lttng/ust/jul/LTTngTCPSessiondClient.java         | 15
 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +3 -1
index 25d1cb2..0d9a485 100644
@@ -34,6 +34,7 @@ import java.util.List;
 import java.util.Timer;
 import java.util.TimerTask;
 import java.util.logging.Logger;
+import java.util.Collections;
 
 class USTRegisterMsg {
      public static int pid;
@@ -57,7 +58,8 @@ public class LTTngTCPSessiondClient {
      private Semaphore registerSem;
 
      private Timer eventTimer;
-     private List<LTTngEvent> enabledEventList = new ArrayList<LTTngEvent>();
+     private List<LTTngEvent> enabledEventList = Collections.synchronizedList(
+             new ArrayList<LTTngEvent>());
      /*
       * Map of Logger objects that have been enabled. They are indexed by name.
       */

liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java public class LTTngTCPSessiondClient { · line 94

Quoted 8 lines
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +4 -1
@@ -86,7 +88,10 @@ public class LTTngTCPSessiondClient {
                               * We have to make a copy here since it is possible that the
                               * enabled event list is changed during an iteration on it.
                               */
-                             List<LTTngEvent> tmpList = new ArrayList<LTTngEvent>(enabledEventList);
+                             List<LTTngEvent> tmpList;
+                             synchronized(enabledEventList) {
+                                     tmpList = new ArrayList<LTTngEvent>(enabledEventList);
+                             }

Further down in this function, we find:

Quoted 7 lines
                                        ret = enableCmd.enableLogger(handler, event, enabledLoggers);
                                        if (ret == 1) {
                                                /* Enabled so remove the event from the list. */
                                                enabledEventList.remove(event);
                                        }

should we encompass a larger part of this function with synchronize ?

AFAIU, this operation is already synchronized by the synchronizedList class.

Quoted 1 line

What happens if event is removed from enabledEventList while we iterate on the list copy ?

I'm not sure I understand your concern.

Regards,
Jérémie

Quoted 36 lines

Thanks,

MAthieu

Quoted 20 lines
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +4 -3
                              LTTngSessiondCmd2_4.sessiond_enable_handler enableCmd = new
                                      LTTngSessiondCmd2_4.sessiond_enable_handler();
@@ -277,8 +282,10 @@ public class LTTngTCPSessiondClient {
                                               * Add the event to the list so it can be enabled if
                                               * the logger appears at some point in time.
                                               */
-                                             if (enabledEventList.contains(event) == false) {
-                                                     enabledEventList.add(event);
+                                             synchronized (enabledEventList) {
+                                                     if (enabledEventList.contains(event) == false) {
+                                                             enabledEventList.add(event);
+                                                     }
                                              }
                                      }
                                      data = enableCmd.getBytes();
--

1.9.0

Quoted 5 lines

_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

Signature · 3 lines
Jérémie Galarneau
EfficiOS Inc.
http://www.efficios.com

A message that is not in the archive (sent off-list or not yet received): CA+jJMxs549OwuD6QBvF6nHW8M0tHyZxcMb0oHJ6okCt4TdoY_Q@mail.gmail.com

#5 lore raw

----- Original Message -----

Quoted 80 lines

From: "Jérémie Galarneau" <[email protected]>
To: "Mathieu Desnoyers" <[email protected]>
Cc: "David Goulet" <[email protected]>, [email protected]
Sent: Saturday, February 22, 2014 2:15:15 PM
Subject: Re: [lttng-dev] [PATCH lttng-ust] Fix: Unsynchronized access in LTTngTCPSessiondClient

Quoted 72 lines · Mathieu Desnoyers

----- Original Message -----

Quoted 56 lines

From: "Jérémie Galarneau" <[email protected]>
To: [email protected]
Sent: Thursday, February 20, 2014 11:22:33 AM
Subject: [lttng-dev] [PATCH lttng-ust] Fix: Unsynchronized access in LTTngTCPSessiondClient

enabledEventList is shared between the LTTngThread and eventTimer threads but is not synchronized.

This patch changes enabledEventList's type from an ArrayList to a synchronizedList which implements the same interface.

---
 .../org/lttng/ust/jul/LTTngTCPSessiondClient.java         | 15
 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java b/liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java index 25d1cb2..0d9a485 100644

liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +2 -1
@@ -34,6 +34,7 @@ import java.util.List;
 import java.util.Timer;
 import java.util.TimerTask;
 import java.util.logging.Logger;
+import java.util.Collections;
 
 class USTRegisterMsg {
      public static int pid;
@@ -57,7 +58,8 @@ public class LTTngTCPSessiondClient {
      private Semaphore registerSem;
 
      private Timer eventTimer;
-     private List<LTTngEvent> enabledEventList = new
 ArrayList<LTTngEvent>();
+     private List<LTTngEvent> enabledEventList =

Collections.synchronizedList(

liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +4 -1
+             new ArrayList<LTTngEvent>());
      /*
       * Map of Logger objects that have been enabled. They are indexed by
       name.
       */
@@ -86,7 +88,10 @@ public class LTTngTCPSessiondClient {
                               * We have to make a copy here since it is
                               possible that the
                               * enabled event list is changed during an
                               iteration on it.
                               */
-                             List<LTTngEvent> tmpList = new
 ArrayList<LTTngEvent>(enabledEventList);
+                             List<LTTngEvent> tmpList;
+                             synchronized(enabledEventList) {
+                                     tmpList = new
Quoted 2 lines

ArrayList<LTTngEvent>(enabledEventList);
+ }

Further down in this function, we find:

                                        ret =
                                        enableCmd.enableLogger(handler,
                                        event, enabledLoggers);
                                        if (ret == 1) {
                                                /* Enabled so remove the
                                                event from the list. */
                                                enabledEventList.remove(event);
                                        }
Quoted 3 lines
Quoted 1 line

should we encompass a larger part of this function with synchronize ?

AFAIU, this operation is already synchronized by the synchronizedList class.

Actually, I got that the remove operation has the synchronize implicit. Also, it should be OK if the event is in the list, and if it is not. Moreover, the concurrent thread only do "add", never remove, so if it was in the list copy, it will be there for the remove.

Quoted 5 lines
Quoted 3 lines

What happens if event is removed from enabledEventList while we iterate on the list copy ?

I'm not sure I understand your concern.

It's OK, see my reply above.

I'll do another reply to your patch for other comments.

Thanks,

Mathieu

Quoted 56 lines

Regards,
Jérémie

Quoted 34 lines

Thanks,

MAthieu

Quoted 28 lines

LTTngSessiondCmd2_4.sessiond_enable_handler enableCmd = new
LTTngSessiondCmd2_4.sessiond_enable_handler();

liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +0 -2
@@ -277,8 +282,10 @@ public class LTTngTCPSessiondClient {
                                               * Add the event to the list
                                               so it can be enabled if
                                               * the logger appears at
                                               some point in time.
                                               */
-                                             if
 (enabledEventList.contains(event) == false) {
-

enabledEventList.add(event);
+ synchronized
(enabledEventList) {
+ if
(enabledEventList.contains(event) == false) {
+
enabledEventList.add(event);
+ }

                                              }
                                      }
                                      data = enableCmd.getBytes();

--
1.9.0

Quoted 10 lines
Quoted 5 lines

_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

--
Jérémie Galarneau
EfficiOS Inc.
http://www.efficios.com

Signature · 8 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
#6 lore raw
In reply to a message not in the archive

Other comments,

----- Original Message -----

Quoted 31 lines

From: "Jérémie Galarneau" <[email protected]>
To: [email protected]
Sent: Thursday, February 20, 2014 11:22:33 AM
Subject: [lttng-dev] [PATCH lttng-ust] Fix: Unsynchronized access in LTTngTCPSessiondClient

enabledEventList is shared between the LTTngThread and eventTimer threads but is not synchronized.

This patch changes enabledEventList's type from an ArrayList to a synchronizedList which implements the same interface.

---
 .../org/lttng/ust/jul/LTTngTCPSessiondClient.java         | 15
 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +1 -0
index 25d1cb2..0d9a485 100644
@@ -34,6 +34,7 @@ import java.util.List;
 import java.util.Timer;
 import java.util.TimerTask;
 import java.util.logging.Logger;
+import java.util.Collections;
 
 class USTRegisterMsg {
 	public static int pid;

liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java public class LTTngTCPSessiondClient { · line 62

Quoted 6 lines
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +2 -1
@@ -57,7 +58,8 @@ public class LTTngTCPSessiondClient {
 	private Semaphore registerSem;
 
 	private Timer eventTimer;
-	private List<LTTngEvent> enabledEventList = new ArrayList<LTTngEvent>();
+	private List<LTTngEvent> enabledEventList = Collections.synchronizedList(
+		new ArrayList<LTTngEvent>());

I'd prefer:

  private List<LTTngEvent> enabledEventList =
         Collections.synchronizedList(new ArrayList<LTTngEvent>());

unless you tell me that your coding style is more "java-like".

liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java public class LTTngTCPSessiondClient { · line 92

Quoted 10 lines
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +2 -1
 	/*
 	 * Map of Logger objects that have been enabled. They are indexed by name.
 	 */
@@ -86,7 +88,10 @@ public class LTTngTCPSessiondClient {
 				 * We have to make a copy here since it is possible that the
 				 * enabled event list is changed during an iteration on it.
 				 */
-				List<LTTngEvent> tmpList = new ArrayList<LTTngEvent>(enabledEventList);
+				List<LTTngEvent> tmpList;
+				synchronized(enabledEventList) {

inconsistent style, missing space after "synchronized".

Quoted 6 lines
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +2 -0
+					tmpList = new ArrayList<LTTngEvent>(enabledEventList);
+				}
 
 				LTTngSessiondCmd2_4.sessiond_enable_handler enableCmd = new
 					LTTngSessiondCmd2_4.sessiond_enable_handler();

liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java public class LTTngTCPSessiondClient { · line 288

Quoted 9 lines
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +4 -2
@@ -277,8 +282,10 @@ public class LTTngTCPSessiondClient {
 						 * Add the event to the list so it can be enabled if
 						 * the logger appears at some point in time.
 						 */
-						if (enabledEventList.contains(event) == false) {
-							enabledEventList.add(event);
+						synchronized (enabledEventList) {
+							if (enabledEventList.contains(event) == false) {
+								enabledEventList.add(event);
+							}

Hrm, so what we want here is probably more a set than a list ? We want a data structure that does not have duplicates. If we can confirm that the order of the elements does not matter, a hash map might be a much more suited data structure than a list. We could then remove the explicit synchronize around "add".

Thanks,

Mathieu

Quoted 12 lines
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +0 -1
 						}
 					}
 					data = enableCmd.getBytes();
--

1.9.0

_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

Signature · 8 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): [email protected]

#8 lore raw
Quoted 73 lines · Mathieu Desnoyers

Other comments,

----- Original Message -----

Quoted 31 lines

From: "Jérémie Galarneau" <[email protected]>
To: [email protected]
Sent: Thursday, February 20, 2014 11:22:33 AM
Subject: [lttng-dev] [PATCH lttng-ust] Fix: Unsynchronized access in LTTngTCPSessiondClient

enabledEventList is shared between the LTTngThread and eventTimer threads but is not synchronized.

This patch changes enabledEventList's type from an ArrayList to a synchronizedList which implements the same interface.

---
 .../org/lttng/ust/jul/LTTngTCPSessiondClient.java         | 15
 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +1 -0
index 25d1cb2..0d9a485 100644
@@ -34,6 +34,7 @@ import java.util.List;
 import java.util.Timer;
 import java.util.TimerTask;
 import java.util.logging.Logger;
+import java.util.Collections;
 
 class USTRegisterMsg {
      public static int pid;

liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java public class LTTngTCPSessiondClient { · line 62

Quoted 6 lines
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +2 -1
@@ -57,7 +58,8 @@ public class LTTngTCPSessiondClient {
      private Semaphore registerSem;
 
      private Timer eventTimer;
-     private List<LTTngEvent> enabledEventList = new ArrayList<LTTngEvent>();
+     private List<LTTngEvent> enabledEventList = Collections.synchronizedList(
+             new ArrayList<LTTngEvent>());

I'd prefer:

  private List<LTTngEvent> enabledEventList =
         Collections.synchronizedList(new ArrayList<LTTngEvent>());

unless you tell me that your coding style is more "java-like".

liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java public class LTTngTCPSessiondClient { · line 92

Quoted 10 lines
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +2 -1
      /*
       * Map of Logger objects that have been enabled. They are indexed by name.
       */
@@ -86,7 +88,10 @@ public class LTTngTCPSessiondClient {
                               * We have to make a copy here since it is possible that the
                               * enabled event list is changed during an iteration on it.
                               */
-                             List<LTTngEvent> tmpList = new ArrayList<LTTngEvent>(enabledEventList);
+                             List<LTTngEvent> tmpList;
+                             synchronized(enabledEventList) {

inconsistent style, missing space after "synchronized".

Quoted 12 lines
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +3 -2
+                                     tmpList = new ArrayList<LTTngEvent>(enabledEventList);
+                             }
 
                              LTTngSessiondCmd2_4.sessiond_enable_handler enableCmd = new
                                      LTTngSessiondCmd2_4.sessiond_enable_handler();
@@ -277,8 +282,10 @@ public class LTTngTCPSessiondClient {
                                               * Add the event to the list so it can be enabled if
                                               * the logger appears at some point in time.
                                               */
-                                             if (enabledEventList.contains(event) == false) {
-                                                     enabledEventList.add(event);
+                                             synchronized (enabledEventList) {
Quoted 8 lines

liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java public class LTTngTCPSessiondClient { · line 288

Quoted 3 lines
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +3 -0
+                                                     if (enabledEventList.contains(event) == false) {
+                                                             enabledEventList.add(event);
+                                                     }

Hrm, so what we want here is probably more a set than a list ? We want a data structure that does not have duplicates. If we can confirm that the order of the elements does not matter, a hash map might be a much more suited data structure than a list. We could then remove the explicit synchronize around "add".

Makes sense, I guess David (original author) will probably have an opinion on that. My immediate concern was addressing the code's thread safety.

Regards,
Jérémie

Quoted 22 lines

Thanks,

Mathieu

Quoted 7 lines
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +0 -1
                                              }
                                      }
                                      data = enableCmd.getBytes();
--

1.9.0

Quoted 5 lines

_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

Signature · 3 lines
Jérémie Galarneau
EfficiOS Inc.
http://www.efficios.com
#9 lore raw

PATCH v2 1/1 Fix: Unsynchronized access in LTTngTCPSessiondClient

In reply to a message not in the archive

enabledEventList is shared between the LTTngThread and eventTimer threads but is not synchronized.

This patch changes enabledEventList's type from an ArrayList to a synchronized HashSet.

---
 .../org/lttng/ust/jul/LTTngTCPSessiondClient.java  | 129 ++++++++++++---------
 1 file changed, 74 insertions(+), 55 deletions(-)
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +74 -55
index 25d1cb2..35f768f 100644
@@ -30,10 +30,14 @@ import java.net.*;
 import java.lang.management.ManagementFactory;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
 import java.util.Timer;
 import java.util.TimerTask;
 import java.util.logging.Logger;
+import java.util.Collections;
 
 class USTRegisterMsg {
 	public static int pid;
@@ -57,7 +61,8 @@ public class LTTngTCPSessiondClient {
 	private Semaphore registerSem;
 
 	private Timer eventTimer;
-	private List<LTTngEvent> enabledEventList = new ArrayList<LTTngEvent>();
+	private Set<LTTngEvent> enabledEventSet =
+		Collections.synchronizedSet(new HashSet<LTTngEvent>());
 	/*
 	 * Map of Logger objects that have been enabled. They are indexed by name.
 	 */
@@ -82,65 +87,81 @@ public class LTTngTCPSessiondClient {
 		this.eventTimer.scheduleAtFixedRate(new TimerTask() {
 			@Override
 			public void run() {
-				/*
-				 * We have to make a copy here since it is possible that the
-				 * enabled event list is changed during an iteration on it.
-				 */
-				List<LTTngEvent> tmpList = new ArrayList<LTTngEvent>(enabledEventList);
-
-				LTTngSessiondCmd2_4.sessiond_enable_handler enableCmd = new
-					LTTngSessiondCmd2_4.sessiond_enable_handler();
-				for (LTTngEvent event: tmpList) {
-					int ret;
-					Logger logger;
-
+				synchronized (enabledEventSet) {
+					LTTngSessiondCmd2_4.sessiond_enable_handler enableCmd = new
+						LTTngSessiondCmd2_4.sessiond_enable_handler();
 					/*
-					 * Check if this Logger name has been enabled already. Note
-					 * that in the case of "*", it's never added in that hash
-					 * table thus the enable command does a lookup for each
-					 * logger name in that hash table for the * case in order
-					 * to make sure we don't enable twice the same logger
-					 * because JUL apparently accepts that the *same*
-					 * LogHandler can be added twice on a Logger object...
-					 * don't ask...
+					 * Modifying events in a Set will raise a
+					 * ConcurrentModificationException. Thus, we remove an event
+					 * and add its modified version to modifiedEvents when a
+					 * modification is necessary.
 					 */
-					logger = enabledLoggers.get(event.name);
-					if (logger != null) {
-						continue;
-					}
+					Set<LTTngEvent> modifiedEvents = new HashSet<LTTngEvent>();
+					Iterator<LTTngEvent> it = enabledEventSet.iterator();
 
-					/*
-					 * Set to one means that the enable all event has been seen
-					 * thus event from that point on must use loglevel for all
-					 * events. Else the object has its own loglevel.
-					 */
-					if (handler.logLevelUseAll == 1) {
-						event.logLevel.level = handler.logLevelAll;
-						event.logLevel.type = handler.logLevelTypeAll;
-					}
+					while (it.hasNext()) {
+						int ret;
+						Logger logger;
+						LTTngEvent event = it.next();
 
-					/*
-					 * The all event is a special case since we have to iterate
-					 * over every Logger to see which one was not enabled.
-					 */
-					if (event.name.equals("*")) {
-						enableCmd.name = event.name;
-						enableCmd.lttngLogLevel = event.logLevel.level;
-						enableCmd.lttngLogLevelType = event.logLevel.type;
 						/*
-						 * The return value is irrelevant since the * event is
-						 * always kept in the list.
+						 * Check if this Logger name has been enabled already. Note
+						 * that in the case of "*", it's never added in that hash
+						 * table thus the enable command does a lookup for each
+						 * logger name in that hash table for the * case in order
+						 * to make sure we don't enable twice the same logger
+						 * because JUL apparently accepts that the *same*
+						 * LogHandler can be added twice on a Logger object...
+						 * don't ask...
 						 */
-						enableCmd.execute(handler, enabledLoggers);
-						continue;
-					}
+						logger = enabledLoggers.get(event.name);
+						if (logger != null) {
+							continue;
+						}
 
-					ret = enableCmd.enableLogger(handler, event, enabledLoggers);
-					if (ret == 1) {
-						/* Enabled so remove the event from the list. */
-						enabledEventList.remove(event);
+						/*
+						 * Set to one means that the enable all event has been seen
+						 * thus event from that point on must use loglevel for all
+						 * events. Else the object has its own loglevel.
+						 */
+						if (handler.logLevelUseAll == 1) {
+							it.remove();
+							event.logLevel.level = handler.logLevelAll;
+							event.logLevel.type = handler.logLevelTypeAll;
+							modifiedEvents.add(event);
+						}
+
+						/*
+						 * The all event is a special case since we have to iterate
+						 * over every Logger to see which one was not enabled.
+						 */
+						if (event.name.equals("*")) {
+							enableCmd.name = event.name;
+							enableCmd.lttngLogLevel = event.logLevel.level;
+							enableCmd.lttngLogLevelType = event.logLevel.type;
+							/*
+							 * The return value is irrelevant since the * event is
+							 * always kept in the set.
+							 */
+							enableCmd.execute(handler, enabledLoggers);
+							continue;
+						}
+
+						ret = enableCmd.enableLogger(handler, event, enabledLoggers);
+						if (ret == 1) {
+							/* Enabled so remove the event from the set. */
+							if (!modifiedEvents.remove(event)) {
+								/*
+								 * event can only be present in one of
+								 * the sets.
+								 */
+								it.remove();
+							}
+						}
 					}
+					enabledEventSet.addAll(modifiedEvents);
 				}
+
 			}
 		}, this.timerDelay, this.timerDelay);
 
@@ -274,12 +295,10 @@ public class LTTngTCPSessiondClient {
 					event = enableCmd.execute(this.handler, this.enabledLoggers);
 					if (event != null) {
 						/*
-						 * Add the event to the list so it can be enabled if
+						 * Add the event to the set so it can be enabled if
 						 * the logger appears at some point in time.
 						 */
-						if (enabledEventList.contains(event) == false) {
-							enabledEventList.add(event);
-						}
+						enabledEventSet.add(event);
 					}
 					data = enableCmd.getBytes();
 					break;
Signature · 7 lines
1.9.0


_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): [email protected]

author Applied
#11 lore raw

merged, thanks!

Mathieu

----- Original Message -----

Quoted 203 lines

From: "Jérémie Galarneau" <[email protected]>
To: [email protected]
Sent: Tuesday, February 25, 2014 4:32:05 PM
Subject: [lttng-dev] [PATCH lttng-ust v2] Fix: Unsynchronized access in LTTngTCPSessiondClient

enabledEventList is shared between the LTTngThread and eventTimer threads but is not synchronized.

This patch changes enabledEventList's type from an ArrayList to a synchronized HashSet.

---
 .../org/lttng/ust/jul/LTTngTCPSessiondClient.java  | 129
 ++++++++++++---------
 1 file changed, 74 insertions(+), 55 deletions(-)
liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +74 -56
index 25d1cb2..35f768f 100644
@@ -30,10 +30,14 @@ import java.net.*;
 import java.lang.management.ManagementFactory;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
 import java.util.Timer;
 import java.util.TimerTask;
 import java.util.logging.Logger;
+import java.util.Collections;
 
 class USTRegisterMsg {
 	public static int pid;
@@ -57,7 +61,8 @@ public class LTTngTCPSessiondClient {
 	private Semaphore registerSem;
 
 	private Timer eventTimer;
-	private List<LTTngEvent> enabledEventList = new ArrayList<LTTngEvent>();
+	private Set<LTTngEvent> enabledEventSet =
+		Collections.synchronizedSet(new HashSet<LTTngEvent>());
 	/*
 	 * Map of Logger objects that have been enabled. They are indexed by name.
 	 */
@@ -82,65 +87,81 @@ public class LTTngTCPSessiondClient {
 		this.eventTimer.scheduleAtFixedRate(new TimerTask() {
 			@Override
 			public void run() {
-				/*
-				 * We have to make a copy here since it is possible that the
-				 * enabled event list is changed during an iteration on it.
-				 */
-				List<LTTngEvent> tmpList = new ArrayList<LTTngEvent>(enabledEventList);
-
-				LTTngSessiondCmd2_4.sessiond_enable_handler enableCmd = new
-					LTTngSessiondCmd2_4.sessiond_enable_handler();
-				for (LTTngEvent event: tmpList) {
-					int ret;
-					Logger logger;
-
+				synchronized (enabledEventSet) {
+					LTTngSessiondCmd2_4.sessiond_enable_handler enableCmd = new
+						LTTngSessiondCmd2_4.sessiond_enable_handler();
 					/*
-					 * Check if this Logger name has been enabled already. Note
-					 * that in the case of "*", it's never added in that hash
-					 * table thus the enable command does a lookup for each
-					 * logger name in that hash table for the * case in order
-					 * to make sure we don't enable twice the same logger
-					 * because JUL apparently accepts that the *same*
-					 * LogHandler can be added twice on a Logger object...
-					 * don't ask...
+					 * Modifying events in a Set will raise a
+					 * ConcurrentModificationException. Thus, we remove an event
+					 * and add its modified version to modifiedEvents when a
+					 * modification is necessary.
 					 */
-					logger = enabledLoggers.get(event.name);
-					if (logger != null) {
-						continue;
-					}
+					Set<LTTngEvent> modifiedEvents = new HashSet<LTTngEvent>();
+					Iterator<LTTngEvent> it = enabledEventSet.iterator();
 
-					/*
-					 * Set to one means that the enable all event has been seen
-					 * thus event from that point on must use loglevel for all
-					 * events. Else the object has its own loglevel.
-					 */
-					if (handler.logLevelUseAll == 1) {
-						event.logLevel.level = handler.logLevelAll;
-						event.logLevel.type = handler.logLevelTypeAll;
-					}
+					while (it.hasNext()) {
+						int ret;
+						Logger logger;
+						LTTngEvent event = it.next();
 
-					/*
-					 * The all event is a special case since we have to iterate
-					 * over every Logger to see which one was not enabled.
-					 */
-					if (event.name.equals("*")) {
-						enableCmd.name = event.name;
-						enableCmd.lttngLogLevel = event.logLevel.level;
-						enableCmd.lttngLogLevelType = event.logLevel.type;
 						/*
-						 * The return value is irrelevant since the * event is
-						 * always kept in the list.
+						 * Check if this Logger name has been enabled already. Note
+						 * that in the case of "*", it's never added in that hash
+						 * table thus the enable command does a lookup for each
+						 * logger name in that hash table for the * case in order
+						 * to make sure we don't enable twice the same logger
+						 * because JUL apparently accepts that the *same*
+						 * LogHandler can be added twice on a Logger object...
+						 * don't ask...
 						 */
-						enableCmd.execute(handler, enabledLoggers);
-						continue;
-					}
+						logger = enabledLoggers.get(event.name);
+						if (logger != null) {
+							continue;
+						}
 
-					ret = enableCmd.enableLogger(handler, event, enabledLoggers);
-					if (ret == 1) {
-						/* Enabled so remove the event from the list. */
-						enabledEventList.remove(event);
+						/*
+						 * Set to one means that the enable all event has been seen
+						 * thus event from that point on must use loglevel for all
+						 * events. Else the object has its own loglevel.
+						 */
+						if (handler.logLevelUseAll == 1) {
+							it.remove();
+							event.logLevel.level = handler.logLevelAll;
+							event.logLevel.type = handler.logLevelTypeAll;
+							modifiedEvents.add(event);
+						}
+
+						/*
+						 * The all event is a special case since we have to iterate
+						 * over every Logger to see which one was not enabled.
+						 */
+						if (event.name.equals("*")) {
+							enableCmd.name = event.name;
+							enableCmd.lttngLogLevel = event.logLevel.level;
+							enableCmd.lttngLogLevelType = event.logLevel.type;
+							/*
+							 * The return value is irrelevant since the * event is
+							 * always kept in the set.
+							 */
+							enableCmd.execute(handler, enabledLoggers);
+							continue;
+						}
+
+						ret = enableCmd.enableLogger(handler, event, enabledLoggers);
+						if (ret == 1) {
+							/* Enabled so remove the event from the set. */
+							if (!modifiedEvents.remove(event)) {
+								/*
+								 * event can only be present in one of
+								 * the sets.
+								 */
+								it.remove();
+							}
+						}
 					}
+					enabledEventSet.addAll(modifiedEvents);
 				}
+
 			}
 		}, this.timerDelay, this.timerDelay);
 
@@ -274,12 +295,10 @@ public class LTTngTCPSessiondClient {
 					event = enableCmd.execute(this.handler, this.enabledLoggers);
 					if (event != null) {
 						/*
-						 * Add the event to the list so it can be enabled if
+						 * Add the event to the set so it can be enabled if
 						 * the logger appears at some point in time.
 						 */
-						if (enabledEventList.contains(event) == false) {
-							enabledEventList.add(event);
-						}
+						enabledEventSet.add(event);
 					}
 					data = enableCmd.getBytes();
 					break;
--

1.9.0

_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

Signature · 8 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev