Skip to content

Mismatch of the PrintWriter in ServletResponse [SPR-13126] #17717

@spring-projects-issues

Description

@spring-projects-issues

Xian Li opened SPR-13126 and commented

javax.servlet.ServletResponse#getWriter will return java.io.PrintWriter.

Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError().

But the renders of popular templates (FreeMarker/Velocity) just use java.io.Writer and expect an IOException thrown if something is wrong.

This mismatch will lead the render writes data continuously while the connection has been broken.

I have met a bug while using default Jetty 8 and FreeMarker. If the client close the connection, The render will write data continuously and the Jetty 8 continuously flush a full buffer. This will lead a performance issue dramatically.

Fixing It depends on the servlet container to return a subclass of java.io.PrintWriter, which can handle exception properly while the connection is broken and the render don't known that.

For popular containers:

  • Jetty 8 will swallow the IOException by default, while you can config to use a UncheckedPrintWriter.
  • Jetty 9 (latest 9.2.11.v20150529) swallow the IOException by default, and you can't change it. This bug has been fixed and will release in jetty 9.3. (https://bugs.eclipse.org/bugs/show_bug.cgi?id=465754)
  • Tomcat will swallow the IOException. But it will abandon the Writer and do nothing further.

But I think maybe the spring framework has the responsibility to handle it because the contract of the ServletResponse declare PrintWriter. You should not assume the web container has handled the mismatch properly.


Affects: 4.2 RC1

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: bulk-closedAn outdated, unresolved issue that's closed in bulk as part of a cleaning process

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions