anoopj commented on code in PR #16031: URL: https://github.com/apache/iceberg/pull/16031#discussion_r3117933915
########## spark/v4.1/spark/src/test/java/org/apache/iceberg/spark/TestCommitMetadata.java: ########## @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.iceberg.spark; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.lang.reflect.Field; +import java.util.Map; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.junit.jupiter.api.Test; + +public class TestCommitMetadata { + + @Test + public void testThreadLocalCleanedUpAfterCommit() throws Exception { + Map<String, String> props = ImmutableMap.of("key", "value"); + + CommitMetadata.withCommitProperties(props, () -> null, RuntimeException.class); + + // After withCommitProperties returns, the ThreadLocal entry should be removed. + // Use reflection to access the private COMMIT_PROPERTIES field and verify + // that the underlying ThreadLocal slot was removed (not just set to empty). + Field field = CommitMetadata.class.getDeclaredField("COMMIT_PROPERTIES"); + field.setAccessible(true); + @SuppressWarnings("unchecked") + ThreadLocal<Map<String, String>> threadLocal = Review Comment: This all seem like dead code and doesn't seem to exercise the fix? ########## spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/CommitMetadata.java: ########## @@ -56,7 +56,7 @@ public static <R, E extends Exception> R withCommitProperties( ExceptionUtil.castAndThrow(e, exClass); return null; } finally { - COMMIT_PROPERTIES.set(ImmutableMap.of()); + COMMIT_PROPERTIES.remove(); Review Comment: The fix looks correct. remove() is the recommended cleanup pattern for ThreadLocals. But could you add some details about the impact of the issue? It seems a single ThreadLocal entry with a Immutable map per thread? That doesn't seem like a significant leak. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
