Skip to content

Commit

Permalink
[audio] audio servlet: don't change map while used by interation (ope…
Browse files Browse the repository at this point in the history
…nhab#801)

We should not remove entries (so keys) from the map while we are using
the key set (a view) of that map (see JavaDoc: otherwise "results of the
iteration undefined").

Signed-off-by: Markus Rathgeb <maggu2810@gmail.com>
GitOrigin-RevId: d19ee33
  • Loading branch information
maggu2810 authored and splatch committed Jul 11, 2023
1 parent 50986a7 commit a2ec8f1
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -143,16 +146,20 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
}

private synchronized void removeTimedOutStreams() {
for (String streamId : multiTimeStreams.keySet()) {
if (streamTimeouts.get(streamId) < System.nanoTime()) {
// the stream has expired, we need to remove it!
FixedLengthAudioStream stream = multiTimeStreams.remove(streamId);
streamTimeouts.remove(streamId);
IOUtils.closeQuietly(stream);
stream = null;
logger.debug("Removed timed out stream {}", streamId);
// Build list of expired streams.
final List<String> toRemove = new LinkedList<>();
for (Entry<String, Long> entry : streamTimeouts.entrySet()) {
if (entry.getValue() < System.nanoTime()) {
toRemove.add(entry.getKey());
}
}
toRemove.forEach(streamId -> {
// the stream has expired, we need to remove it!
final FixedLengthAudioStream stream = multiTimeStreams.remove(streamId);
streamTimeouts.remove(streamId);
IOUtils.closeQuietly(stream);
logger.debug("Removed timed out stream {}", streamId);
});
}

@Override
Expand Down

0 comments on commit a2ec8f1

Please sign in to comment.