You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openwebbeans.apache.org by Adam Cornett <ad...@gmail.com> on 2015/06/11 01:13:50 UTC

WebContextsService errors when servlet session invalidated during request lifecycle

If the session is invalidated during the processing of the request an
exception is thrown when OpenWebBeans is cleaning up.

The issue is in the WebContextsService.destroyRequestContext method, which
tries to get the session from the request, however it calls the no-arg
getSession()
<http://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/http/HttpServletRequest.html#getSession()>,
which will, per the spec, try to create a new session if one does not
exist.  However since the response has been committed this is not allowed
to create a new session and an exception is thrown.
The solution is to call getSession(false)
<http://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/http/HttpServletRequest.html#getSession(boolean)>,
which will return null if the session does not exist.  This seems to be the
desired behavior anyway since the result of the call is null checked a few
lines later.

I have tested this change in our application and it produces the desired
behavior.

Current code:


            Object payload = null;
            if (context.getServletRequest() != null)
            {
                payload = context.getServletRequest().getSession();
            }

            webBeansContext.getBeanManagerImpl().fireContextLifecyleEvent(
                payload != null ? payload : new Object(),
DestroyedLiteral.INSTANCE_SESSION_SCOPED);

Suggested fix:


            Object payload = null;
            if (context.getServletRequest() != null)
            {
                payload = context.getServletRequest().getSession(false);
            }

            webBeansContext.getBeanManagerImpl().fireContextLifecyleEvent(
                payload != null ? payload : new Object(),
DestroyedLiteral.INSTANCE_SESSION_SCOPED);

-- 
Adam Cornett
adam.cornett@gmail.com
(678) 296-1150

Re: WebContextsService errors when servlet session invalidated during request lifecycle

Posted by Mark Struberg <st...@yahoo.de>.
+1 and txs for reporting!

LieGrue,
strub

> Am 11.06.2015 um 15:07 schrieb Thomas Andraschko <an...@gmail.com>:
> 
> +1
> please create a issue
> 
> 2015-06-11 1:13 GMT+02:00 Adam Cornett <ad...@gmail.com>:
> 
>> If the session is invalidated during the processing of the request an
>> exception is thrown when OpenWebBeans is cleaning up.
>> 
>> The issue is in the WebContextsService.destroyRequestContext method, which
>> tries to get the session from the request, however it calls the no-arg
>> getSession()
>> <
>> http://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/http/HttpServletRequest.html#getSession()
>>> ,
>> which will, per the spec, try to create a new session if one does not
>> exist.  However since the response has been committed this is not allowed
>> to create a new session and an exception is thrown.
>> The solution is to call getSession(false)
>> <
>> http://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/http/HttpServletRequest.html#getSession(boolean)
>>> ,
>> which will return null if the session does not exist.  This seems to be the
>> desired behavior anyway since the result of the call is null checked a few
>> lines later.
>> 
>> I have tested this change in our application and it produces the desired
>> behavior.
>> 
>> Current code:
>> 
>> 
>>            Object payload = null;
>>            if (context.getServletRequest() != null)
>>            {
>>                payload = context.getServletRequest().getSession();
>>            }
>> 
>>            webBeansContext.getBeanManagerImpl().fireContextLifecyleEvent(
>>                payload != null ? payload : new Object(),
>> DestroyedLiteral.INSTANCE_SESSION_SCOPED);
>> 
>> Suggested fix:
>> 
>> 
>>            Object payload = null;
>>            if (context.getServletRequest() != null)
>>            {
>>                payload = context.getServletRequest().getSession(false);
>>            }
>> 
>>            webBeansContext.getBeanManagerImpl().fireContextLifecyleEvent(
>>                payload != null ? payload : new Object(),
>> DestroyedLiteral.INSTANCE_SESSION_SCOPED);
>> 
>> --
>> Adam Cornett
>> adam.cornett@gmail.com
>> (678) 296-1150
>> 


Re: WebContextsService errors when servlet session invalidated during request lifecycle

Posted by Thomas Andraschko <an...@gmail.com>.
+1
please create a issue

2015-06-11 1:13 GMT+02:00 Adam Cornett <ad...@gmail.com>:

> If the session is invalidated during the processing of the request an
> exception is thrown when OpenWebBeans is cleaning up.
>
> The issue is in the WebContextsService.destroyRequestContext method, which
> tries to get the session from the request, however it calls the no-arg
> getSession()
> <
> http://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/http/HttpServletRequest.html#getSession()
> >,
> which will, per the spec, try to create a new session if one does not
> exist.  However since the response has been committed this is not allowed
> to create a new session and an exception is thrown.
> The solution is to call getSession(false)
> <
> http://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/http/HttpServletRequest.html#getSession(boolean)
> >,
> which will return null if the session does not exist.  This seems to be the
> desired behavior anyway since the result of the call is null checked a few
> lines later.
>
> I have tested this change in our application and it produces the desired
> behavior.
>
> Current code:
>
>
>             Object payload = null;
>             if (context.getServletRequest() != null)
>             {
>                 payload = context.getServletRequest().getSession();
>             }
>
>             webBeansContext.getBeanManagerImpl().fireContextLifecyleEvent(
>                 payload != null ? payload : new Object(),
> DestroyedLiteral.INSTANCE_SESSION_SCOPED);
>
> Suggested fix:
>
>
>             Object payload = null;
>             if (context.getServletRequest() != null)
>             {
>                 payload = context.getServletRequest().getSession(false);
>             }
>
>             webBeansContext.getBeanManagerImpl().fireContextLifecyleEvent(
>                 payload != null ? payload : new Object(),
> DestroyedLiteral.INSTANCE_SESSION_SCOPED);
>
> --
> Adam Cornett
> adam.cornett@gmail.com
> (678) 296-1150
>