# java.util.ConcurrentModificationException during client.getPlayStreams() iteration

**URL:** <https://community.wowza.com/t/java-util-concurrentmodificationexception-during-client-getplaystreams-iteration/37158>\
**Category:** Wowza Developer Dojo\
**Tags:** wowza-streaming-server-java-api\
**Created:** [May 26, 2014, 11:44am UTC](https://community.wowza.com/t/java-util-concurrentmodificationexception-during-client-getplaystreams-iteration/37158 "2014-05-26T11:44:49Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Pokotilo](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Alex\_Pokotilo](https://community.wowza.com/u/Alex_Pokotilo)\
**Post date:** [May 26, 2014, 11:44am UTC](https://community.wowza.com/t/java-util-concurrentmodificationexception-during-client-getplaystreams-iteration/37158/1 "2014-05-26T11:44:49Z")

</div>

This strange case reproduced for only one my client’s server. Server version is 3.6.4 build9641. I have following code

```auto
for(String vHostName: (List<String>)server.getVHostList().getVHostNames())
{
	IVHost vhost = VHostSingleton.getInstance(vHostName);
	if(vhost==null)continue;
	for(String appName: (List<String>)vhost.getApplicationNames()){
		IApplication application = vhost.getApplication((String) appName);
		if(application == null) continue;
		for(String appIntanceName : application.getAppInstanceNames()){
			IApplicationInstance appIntance = application.getAppInstance(appIntanceName);
			if(appIntance == null) continue;
			for(IClient client: appIntance.getClients())
			{
                               // Exception: java.util.ConcurrentModificationException|at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)|at java.util.ArrayList$Itr.next(ArrayList.java:831)|
				for(IMediaStream stream : (List<IMediaStream>)client.getPlayStreams()){  
				}
			}
		}
	}
}

```

so it looks like client.getPlayStreams() changed outside the look in different thread. All examples in wowza articles use the same approach and client.getPlayStreams() not locked or syncronized before iteration. why this happends and what should I do to prevent this case? or may be i need to handle it and retry ?

---

<div class="post-metadata">

**Author:** ![Matt\_Young](https://sea2.discourse-cdn.com/flex002/user_avatar/community.wowza.com/matt_young/32/389_2.png) [@Matt\_Young](https://community.wowza.com/u/Matt_Young)\
**Post date:** [May 27, 2014, 5:13am UTC](https://community.wowza.com/t/java-util-concurrentmodificationexception-during-client-getplaystreams-iteration/37158/2 "2014-05-27T05:13:02Z")

</div>

I think to avoid this error, you will want to get the playstreams into a list and utilize the list. Something more like the following:

```auto
List<IMediaStream> playStreams = client.getPlayStreams(); 
Iterator iterPlay = playStreams.iterator(); 
while (iterPlay.hasNext()) 
{ 
   IMediaStream stream = (IMediaStream)iterPlay.next(); 
   // .. etc ..
} 

```

---

<div class="post-metadata">

**Author:** ![Alex\_Pokotilo](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Alex\_Pokotilo](https://community.wowza.com/u/Alex_Pokotilo)\
**Post date:** [May 27, 2014, 9:03pm UTC](https://community.wowza.com/t/java-util-concurrentmodificationexception-during-client-getplaystreams-iteration/37158/3 "2014-05-27T21:03:49Z")

</div>

> I think to avoid this error, you will want to get the playstreams into a list and utilize the list. Something more like the following:
> 
> ```auto
> List<IMediaStream> playStreams = client.getPlayStreams(); 
> Iterator iterPlay = playStreams.iterator(); 
> while (iterPlay.hasNext()) 
> { 
> IMediaStream stream = (IMediaStream)iterPlay.next(); 
> // .. etc ..
> } 
> 
> ```

Matt thank you for reply.

Are you sure about this ?

This is from callstack

Exception: java.util.ConcurrentModificationException

at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)

at java.util.ArrayList$Itr.next(ArrayList.java:831)

so construction I use and you proposed are equal. Please ask from Development team why java.util.ConcurrentModificationException can be thrown

---

<div class="post-metadata">

**Author:** ![Alex\_Pokotilo](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Alex\_Pokotilo](https://community.wowza.com/u/Alex_Pokotilo)\
**Post date:** [May 28, 2014, 6:22pm UTC](https://community.wowza.com/t/java-util-concurrentmodificationexception-during-client-getplaystreams-iteration/37158/4 "2014-05-28T18:22:32Z")

</div>

> Where are you implementing this functionality? Is it, by chance, in a play event handler?

This functionality implemented in separate thread and not in any wowza callback. It checks usage statistics periodically.

Why it’s matter there this functionality implemented ? I saw wowza examples working in separate threads and doing the same

---

<div class="post-metadata">

**Author:** ![Alex\_Pokotilo](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Alex\_Pokotilo](https://community.wowza.com/u/Alex_Pokotilo)\
**Post date:** [May 30, 2014, 8:06am UTC](https://community.wowza.com/t/java-util-concurrentmodificationexception-during-client-getplaystreams-iteration/37158/5 "2014-05-30T08:06:21Z")

</div>

> I’m trying to get a contextual feel for whats going on as I’m trying to replicate this locally and haven’t had success in doing so thus far. Are you able to reproduce this in latter versions of Wowza (4+)?
> 
> Thanks,
> 
> Matt

It’s not very common as our module used for years and this client’s first who found this. I cannot reproduce this case but I think we shouldn’t try to reproduce it but rather check in what cases this problem can occur and what should we do in case we get it.

My guess about why this happens is because rtmp support multiple streams and depending to application clients use wowza client can add new streams during livetime. So let’s consider a case when connected client get new stream when I iterate it’s streams in my thread. I think wowza server doesn’t duplication iterator before return it via api so it’s possible that client add or remove stream during iteration in different thread. Currently I’m working with client to check this guess possibility but could you please work with wowza core development team to get an answer to this case. Currently I see only one possible solution: handle this exception and retry. Could you please propose something else ? Or may be my proposed approach is dangerous in some cases ?

---

<div class="post-metadata">

**Author:** ![Alex\_Pokotilo](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Alex\_Pokotilo](https://community.wowza.com/u/Alex_Pokotilo)\
**Post date:** [May 30, 2014, 9:11am UTC](https://community.wowza.com/t/java-util-concurrentmodificationexception-during-client-getplaystreams-iteration/37158/6 "2014-05-30T09:11:23Z")

</div>

> Hi,
> 
> You need to synchronize the list of play streams when you are going to iterate through it. The following should work,
> 
> ```auto
> for(IClient client: appInstance.getClients()) // appInstance.getClients(); returns a copy of the list so shouldn't need to be synchronized.
> {
> List<IMediaStream> playStreams = client.getPlayStreams(); // client.getPlayStreams(); returns the live list so does need synchronizing.
> synchronized(playStreams)
> {
> for(IMediaStream stream : playStreams)
> {
> // do something here
> }
> }
> }
> 
> ```
> 
> Roger.

Roger,

thanks as always ! So you confirm wowza itself do synchronized(playStreams) before change client.getPlayStreams() ?

---

<div class="post-metadata">

**Author:** ![Alex\_Pokotilo](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Alex\_Pokotilo](https://community.wowza.com/u/Alex_Pokotilo)\
**Post date:** [May 30, 2014, 5:08pm UTC](https://community.wowza.com/t/java-util-concurrentmodificationexception-during-client-getplaystreams-iteration/37158/7 "2014-05-30T17:08:54Z")

</div>

> Hi,
> 
> Yes that is correct. The internal methods synchronize on the actual playStreams list before modifying it so you should do the same.
> 
> Roger.

Roger,

thanks a lot for clarifications !

what do you think about exception handling in this case ? so if collection externally modified I’ll get exception handle it and try to get lock and retry then but if it’s not I’ll not block collection for wowza internal use. from my POV such retries more efficient. What do you think ?

---

<div class="post-metadata">

**Author:** ![Roger\_Littin1](https://avatars.discourse-cdn.com/v4/letter/r/c89c15/32.png) [@Roger\_Littin1](https://community.wowza.com/u/Roger_Littin1)\
**Post date:** [May 30, 2014, 10:40am UTC](https://community.wowza.com/t/java-util-concurrentmodificationexception-during-client-getplaystreams-iteration/37158/8 "2014-05-30T10:40:37Z")

</div>

Hi,

You need to synchronize the list of play streams when you are going to iterate through it. The following should work,

```auto
	for(IClient client: appInstance.getClients()) // appInstance.getClients(); returns a copy of the list so shouldn't need to be synchronized.
	{
		List<IMediaStream> playStreams = client.getPlayStreams(); // client.getPlayStreams(); returns the live list so does need synchronizing.
		synchronized(playStreams)
		{
			for(IMediaStream stream : playStreams)
			{
				// do something here
			}
		}
	}

```

Roger.

---

<div class="post-metadata">

**Author:** ![Roger\_Littin1](https://avatars.discourse-cdn.com/v4/letter/r/c89c15/32.png) [@Roger\_Littin1](https://community.wowza.com/u/Roger_Littin1)\
**Post date:** [May 30, 2014, 12:00pm UTC](https://community.wowza.com/t/java-util-concurrentmodificationexception-during-client-getplaystreams-iteration/37158/9 "2014-05-30T12:00:44Z")

</div>

Hi,

Yes that is correct. The internal methods synchronize on the actual playStreams list before modifying it so you should do the same.

Roger.

---

<div class="post-metadata">

**Author:** ![Matt\_Young](https://sea2.discourse-cdn.com/flex002/user_avatar/community.wowza.com/matt_young/32/389_2.png) [@Matt\_Young](https://community.wowza.com/u/Matt_Young)\
**Post date:** [May 28, 2014, 2:54am UTC](https://community.wowza.com/t/java-util-concurrentmodificationexception-during-client-getplaystreams-iteration/37158/10 "2014-05-28T02:54:37Z")

</div>

Where are you implementing this functionality? Is it, by chance, in a play event handler?

---

<div class="post-metadata">

**Author:** ![Matt\_Young](https://sea2.discourse-cdn.com/flex002/user_avatar/community.wowza.com/matt_young/32/389_2.png) [@Matt\_Young](https://community.wowza.com/u/Matt_Young)\
**Post date:** [May 29, 2014, 5:55am UTC](https://community.wowza.com/t/java-util-concurrentmodificationexception-during-client-getplaystreams-iteration/37158/11 "2014-05-29T05:55:50Z")

</div>

I’m trying to get a contextual feel for whats going on as I’m trying to replicate this locally and haven’t had success in doing so thus far. Are you able to reproduce this in latter versions of Wowza (4+)?

Thanks,

Matt
