From 274eddcb036c301bdaffe0b55ad1a32f6214d11e Mon Sep 17 00:00:00 2001 From: Todd Lipcon Date: Thu, 17 May 2012 04:28:24 +0000 Subject: [PATCH] HADOOP-8406. CompressionCodecFactory.CODEC_PROVIDERS iteration is thread-unsafe. Contributed by Todd Lipcon. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1339475 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-common-project/hadoop-common/CHANGES.txt | 3 +++ .../hadoop/io/compress/CompressionCodecFactory.java | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index befa1c86c1d..207378f6850 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -35,6 +35,9 @@ Release 2.0.1-alpha - UNRELEASED HADOOP-8400. All commands warn "Kerberos krb5 configuration not found" when security is not enabled. (tucu) + HADOOP-8406. CompressionCodecFactory.CODEC_PROVIDERS iteration is + thread-unsafe (todd) + Release 2.0.0-alpha - UNRELEASED INCOMPATIBLE CHANGES diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/CompressionCodecFactory.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/CompressionCodecFactory.java index e12dcfeeb4a..dc95e9e999d 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/CompressionCodecFactory.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/CompressionCodecFactory.java @@ -109,8 +109,12 @@ public class CompressionCodecFactory { List> result = new ArrayList>(); // Add codec classes discovered via service loading - for (CompressionCodec codec : CODEC_PROVIDERS) { - result.add(codec.getClass()); + synchronized (CODEC_PROVIDERS) { + // CODEC_PROVIDERS is a lazy collection. Synchronize so it is + // thread-safe. See HADOOP-8406. + for (CompressionCodec codec : CODEC_PROVIDERS) { + result.add(codec.getClass()); + } } // Add codec classes from configuration String codecsString = conf.get("io.compression.codecs");