File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Problem with GZIPResponseStream hanging at deflateBytes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Problem with GZIPResponseStream hanging at deflateBytes" Watch "Problem with GZIPResponseStream hanging at deflateBytes" New topic
Author

Problem with GZIPResponseStream hanging at deflateBytes

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Howdy,

This is sort of continuation from this thread. In my application I have a Spring controller which serves files to requests. In that controller if there is any exception I send the user to an error page (using ModelAndView of Spring). Now while writing the response JSP page, the response "hangs" at Deflater.deflateBytes method.

Edit: There is a filter applied to each request to Zip/compress the response. The Deflater.deflateBytes is called when the filter tries to compress the response. It works fine everywhere except here.

This is what my code looks like

As far as I could figure out, whenever there is an exception (which most probably is "Connection reset by peer: socket write error"), when I return an error page to the response, the thread hangs at

It starts taking a lot of CPU time. When I searched on the internet, other people also seem to have this problem (with no solution):
http://www.java.net/forum/topic/glassfish/glassfish/glassfish-301-gzip-problem-threads-apparently-spinning-100-cpu-use
http://stackoverflow.com/questions/6584765/thread-locking-when-flushing-jsp-file
https://forums.oracle.com/forums/thread.jspa?messageID=4627097

The problem seems to be with thread safety i.e. two thread writing to the same output stream. My doubt is that while the code is streaming the file to user, if user cancels it/closes the browser window, then it throws some IOException and after that the code tries to write a JSP error page to the same stream (which is already closed) which causes it to go nuts.

Is there a way to avoid this problem or maybe make the deflateBytes timeout somehow (highly unlikely but still )? This problem is not very easy to reproduce so its very hard to apply a fix for it

[Edit: Added some more information]


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
abani patra
Ranch Hand

Joined: Oct 11, 2011
Posts: 70
Hi,
You have not mention the response header and content type , Please try to set it and test.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

I have set the response content type (its not visible in this code). Anyway for now I've excluded this particular path from the GZip filter to solve this problem. Even though that's not a proper solution but it works for now...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with GZIPResponseStream hanging at deflateBytes