Bug 6617 - Iterating over session attribute name enumeration fails with a ConcurrentModificationException on org.apache.catalina.util.Enumerator.nextElement(Enumerator.java:166);
Summary: Iterating over session attribute name enumeration fails with a ConcurrentModi...
Status: RESOLVED DUPLICATE of bug 19103
Alias: None
Product: Tomcat 4
Classification: Unclassified
Component: Catalina (show other bugs)
Version: 4.0.1 Final
Hardware: All other
: P3 critical with 4 votes (vote)
Target Milestone: ---
Assignee: Tomcat Developers Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-02-21 17:42 UTC by Kevin Ross
Modified: 2004-11-16 19:05 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kevin Ross 2002-02-21 17:42:10 UTC
Attempting to clean up the session by iterating over the enumeration of 
attribute names fails with a ConcurrentModificationException on 
org.apache.catalina.util.Enumerator.nextElement(Enumerator.java:166);

=========CODE=====================================

Enumeration enumeration = session.getAttributeNames();

System.out.println("Enum: " + enumeration);
while(enumeration.hasMoreElements()) {

    String name = null;
    try{
        name = (String)enumeration.nextElement();
    }
    catch(Throwable t){


        System.out.println("Caught Throwable while getting next element from 
enumeration. name: " + name);
        t.printStackTrace();
        break;
    }

    System.out.println("Attempting to remove from enumeration: " + name);
    session.removeAttribute(name);
    System.out.println("Done removing from enumeration: " + name + ".");
}



=========LOG======================================
Cleaning up the session...
Enumeration: org.apache.catalina.util.Enumerator@f2225f
Attempting to remove from enumeration: mk
Done removing from enumeration: mk.
Caught Throwable while getting next element from enumeration. name: null
java.util.ConcurrentModificationException
        at java.util.HashMap$HashIterator.nextEntry(HashMap.java:750)
        at java.util.HashMap$KeyIterator.next(HashMap.java:786)
        at org.apache.catalina.util.Enumerator.nextElement(Enumerator.java:166)
        at com.bredex.servlet.Login.service(Login.java:113)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:247)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.StandardWrapperValve.invoke
(StandardWrapperValve.java:243)
        at org.apache.catalina.core.StandardPipeline.invokeNext
(StandardPipeline.java:566)
        at org.apache.catalina.core.StandardPipeline.invoke
(StandardPipeline.java:472)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at org.apache.catalina.core.StandardContextValve.invoke
(StandardContextValve.java:201)
        at org.apache.catalina.core.StandardPipeline.invokeNext
(StandardPipeline.java:566)
        at org.apache.catalina.valves.CertificatesValve.invoke
(CertificatesValve.java:246)
        at org.apache.catalina.core.StandardPipeline.invokeNext
(StandardPipeline.java:564)
        at org.apache.catalina.core.StandardPipeline.invoke
(StandardPipeline.java:472)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at org.apache.catalina.core.StandardContext.invoke
(StandardContext.java:2344)
        at org.apache.catalina.core.StandardHostValve.invoke
(StandardHostValve.java:164)
        at org.apache.catalina.core.StandardPipeline.invokeNext
(StandardPipeline.java:566)
        at org.apache.catalina.valves.ErrorDispatcherValve.invoke
(ErrorDispatcherValve.java:170)
        at org.apache.catalina.core.StandardPipeline.invokeNext
(StandardPipeline.java:564)
        at org.apache.catalina.valves.ErrorReportValve.invoke
(ErrorReportValve.java:170)
        at org.apache.catalina.core.StandardPipeline.invokeNext
(StandardPipeline.java:564)
        at org.apache.catalina.valves.AccessLogValve.invoke
(AccessLogValve.java:462)
        at org.apache.catalina.core.StandardPipeline.invokeNext
(StandardPipeline.java:564)
        at org.apache.catalina.core.StandardPipeline.invoke
(StandardPipeline.java:472)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at org.apache.catalina.core.StandardEngineValve.invoke
(StandardEngineValve.java:163)
        at org.apache.catalina.core.StandardPipeline.invokeNext
(StandardPipeline.java:566)
        at org.apache.catalina.core.StandardPipeline.invoke
(StandardPipeline.java:472)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
        at org.apache.catalina.connector.http.HttpProcessor.process
(HttpProcessor.java:1011)
        at org.apache.catalina.connector.http.HttpProcessor.run
(HttpProcessor.java:1106)
        at java.lang.Thread.run(Thread.java:536)
Comment 1 David Lecomber 2002-03-21 12:41:07 UTC
That's because you're changing the set whilst iterating over it with an
enumerator.  If you clone the list of attribute names and use this to get your
iterator/enumerator, you will be okay..
Comment 2 Carlos Arana 2002-09-29 06:02:53 UTC
It looks like that the Enumeration its cached by the server , for me this is a bug
because after obtaining the enumeration of the attributes i understand that its
totally disconnected from Collection who creates them (probably im wrong).

But i test that code in other servers and work , probably the server returns a
clone.

Carlos arana
Comment 3 Scott Goldstein 2002-10-29 00:02:55 UTC
I've seen this exception as well under different circumstances that are 
definitely valid.  

Suppose that while enumerating through the session attributes, another request 
is made that accesses the same session and removes an attributes.  Since the 
Session attributes are accessed by different threads and different requests, 
the use of the Enumerator is definitely valid.  

This can happen either during stress testing when multiple requests are 
performed for a single user with little time between or when a user opens 
multiple browsers and performs two concurrent requests.

In either case, this is a vliad bug.  The session attributes Collection should 
be cloned before an Enumerator is returned.
Comment 4 Tim Funk 2003-04-21 23:06:52 UTC

*** This bug has been marked as a duplicate of 19103 ***