[MAGNOLIA-3984] A way to prevent "Cannot create a session after the response has been committed" Created: 28/Feb/12  Updated: 03/Dec/13  Resolved: 03/Dec/13

Status: Closed
Project: Magnolia
Component/s: core
Affects Version/s: 4.4.5
Fix Version/s: None

Type: Improvement Priority: Neutral
Reporter: Per Andersson [X] (Inactive) Assignee: Tobias Mattsson
Resolution: Won't Fix Votes: 0
Labels: blossom, session
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

JBoss 5.1 EAP, Oracle 10, Google Chrome 17.0.963.56, Firefox 10.0.1, Ubuntu 11.10


Template:
Acceptance criteria:
Empty
Task DoD:
[ ]* Doc/release notes changes? Comment present?
[ ]* Downstream builds green?
[ ]* Solution information and context easily available?
[ ]* Tests
[ ]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)
Date of First Response:

 Description   

Hi,

Me and my team noticed a very interesting behavior for one of our Spring Webflow components. For some of our browsers it generated the exception: "java.lang.IllegalStateException: Cannot create a session after the response has been committed". This seems to be because the template html is being written to the request output stream before any of our components. When Magnolia iterates and executes our Spring Webflow component it tries to create a session, which by then is too late because data is already being written to the output stream. Although this is only a problem if the end-users browser doesn't support G-Zip and if the cache filter is disabled for a specific path. The supplied browser versions (and OS version) I've supplied above seems to not accept G-zip content which is why I notice this behavior on my computer.

So my proposal is this: Is it possible for you to use a separate OutputStream instance and then write to the real OutputStream when the entire template/page is rendered? We are adding "<#assign session = ${ctx.request.session} />" at the top of the freemarker template file as a workaround at the moment.

Used this forum-post as a reference: http://forum.magnolia-cms.com/forum/thread.html?threadId=5e34ed28-5269-4333-9e9d-a30aca8a95e3



 Comments   
Comment by Richard Unger [ 28/Feb/12 ]

Since I was initiator of the original forum post, I will take the liberty of commenting on this bug.

We managed to solve the problem by converting the paragraph involved to use AJAX and "direct paragraph access", meaning the paragraph gets rendered on its own, without a surrounding template. This solution works well for us now.

I believe (someone at Magnolia can correct me on this) there is now also another solution, involving making your paragraph model implement the marker interface "info.magnolia.module.templating.EarlyExecutionAware". Paragraphs implementing this interface get their execute() method called before the response is committed.

I would not support the solution you describe for the general case - buffering to a separate outputstream would have both a performance and a memory impact. For a site under load this might affect magnolia's overall performance.

Comment by Tobias Mattsson [ 03/Dec/13 ]

Hi Per,

The servlet API is designed this way to allow for efficient streaming of the response. It's not possible to set headers once the response has been committed and since the session relies on setting a cookie as a header it fails in the way you describe.

Buffering to memory would work but I would not recommend it for performance reasons.

I think the best option is to use the pre-execution feature in Blossom to solve your use case. Alternatively you could add a filter that creates a session ahead of page rendering where you know it will be needed.

Generated at Mon Feb 12 03:51:34 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.