Search code examples
javawildflyjava-ioundertow

WIldfly / Undertow UT010029 Stream is closed


running Wildfly 16 on jdk 9 and when repeatedly calling java StringReader, I get UT010029 Stream is closed.

public void export(OutputStream out) throws Exception   {
...
while() { 
  ...
  csvstream = new StringReader(csvcontent.toString());
  try { 
    while ((iRead = csvstream.read()) != -1) out.write(iRead);
    out.flush();
  } catch(IOException e)    { 
    validconnection=false;
    log.error(e);
    e.printStackTrace();
    break;      
  } finally {if (csvstream != null) csvstream.close();}
}

the error happens on csvstream.read()

java.io.IOException: UT010029: Stream is closed
at io.undertow.servlet.spec.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:137)
at io.undertow.servlet.spec.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:122)

I've verified csvstream is not null, but is closed and csvcontent is not null and has a positive length. Sometimes it runs through all the iterations no problem, sometimes it errors out.

It looks like maybe "out" is the stream that is closed. The offending line in ServletOutputStreamImpl.java is:

if (anyAreSet(state, FLAG_CLOSED) || servletRequestContext.getOriginalResponse().isTreatAsCommitted()) {
      throw UndertowServletMessages.MESSAGES.streamIsClosed();

In my case, out is passed in as a parameter. The calling party being a servlet like so:

OutputStream out = response.getOutputStream(  );
if (ve!=null)   {
  try {
        ve.export(out);
    } catch (Exception e)   {
        e.printStackTrace();
    }
} //if ve!=null

ve is a simple POJO.

Any help with this would be appreciated! Thanks.


Solution

  • in case anyone else runs into this - this was solved via wildfly configuration via standalone.xml: the http-listener element has attributes read-timeout and write-timeout. <http-listener ... read-timeout="600000" write-timeout="600000" .../> More recent versions of wildfly clamped down on the default value and these attributes are not specified in the default standalone.xml. See:

    [Wildfly Model Reference][1]