[ 
https://issues.apache.org/jira/browse/HADOOP-9312?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13579636#comment-13579636
 ] 

Chris Nauroth commented on HADOOP-9312:
---------------------------------------

This came up during code review for HADOOP-9232, which adds a native 
implementation for this method on Windows.  It turns out that both the Linux 
implementation and the Windows implementation headed for branch-trunk-win have 
this problem.

Pasting my comments from HADOOP-9232:

Is the following code not thread-safe?

{code}
...
static jobjectArray emptyGroups = NULL;
...
  if (emptyGroups == NULL) {
    jobjectArray lEmptyGroups = (jobjectArray)(*env)->NewObjectArray(env, 0,
            (*env)->FindClass(env, "java/lang/String"), NULL);
    if (lEmptyGroups == NULL) {
      goto cleanup;
    }
    emptyGroups = (*env)->NewGlobalRef(env, lEmptyGroups);
    if (emptyGroups == NULL) {
      goto cleanup;
    }
  }
{code}

For example, assume 2 threads concurrently call getGroupForUser. Thread 1 
executes the NULL check, enters the if body, and then gets suspended by the OS. 
Thread 2 executes and emptyGroups is still NULL, so it initializes it. Then, 
the OS resumes thread 1, which proceeds inside the if body and calls 
NewObjectArray again. Since emptyGroups never gets freed, I believe the net 
effect would be a small memory leak.
                
> JniBasedUnixGroupsMapping#getGroupForUser can potentially leak memory
> ---------------------------------------------------------------------
>
>                 Key: HADOOP-9312
>                 URL: https://issues.apache.org/jira/browse/HADOOP-9312
>             Project: Hadoop Common
>          Issue Type: Bug
>          Components: native, security
>    Affects Versions: 3.0.0, trunk-win
>            Reporter: Chris Nauroth
>
> This method lazily initializes a static variable to contain an empty array of 
> groups.  If multiple threads call the method before the variable has been 
> initialized, then there is potential for a race condition.  This would cause 
> multiple allocations of the array and leaked memory.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to