freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Christian Senkowski (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (FREEMARKER-29) Stream closed Exception
Date Wed, 27 Jul 2016 13:53:20 GMT

    [ https://issues.apache.org/jira/browse/FREEMARKER-29?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15395706#comment-15395706
] 

Christian Senkowski commented on FREEMARKER-29:
-----------------------------------------------

Hi [~ddekany], thanks for pointing out that this exception might have always been there but
ninja'd away.

I found it. 
We were pre-processing includes through Hystrix-Calls, so we had to do some workarounds. 

{quote}
final StringWriter writer = new StringWriter();
final Environment fmEnv = template.createProcessingEnvironment(hash, new StringWriter());
fmEnv.process();
... do more stuff ...
fmEnv.setOut(writer);
fmEnv.process();
{quote}

In the first "process" we saw that Template.java called "close" on StringWriter.

{quote}
java.io.BufferedReader.close(BufferedReader.java:525),
java.io.FilterReader.close(FilterReader.java:121),
freemarker.template.Template$LineTableBuilder.close(Template.java:717),
freemarker.core.SimpleCharStream.FillBuff(SimpleCharStream.java:110),
freemarker.core.SimpleCharStream.readChar(SimpleCharStream.java:188),
freemarker.core.SimpleCharStream.BeginToken(SimpleCharStream.java:129),
freemarker.core.FMParserTokenManager.getNextToken(FMParserTokenManager.java:7190),
freemarker.core.FMParser.jj_scan_token(FMParser.java:5131),
freemarker.core.FMParser.jj_3_16(FMParser.java:3812),
freemarker.core.FMParser.jj_2_16(FMParser.java:3549),
freemarker.core.FMParser.Root(FMParser.java:3427),
freemarker.template.Template.<init>(Template.java:208),
freemarker.cache.TemplateCache.loadTemplate(TemplateCache.java:495),
freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:409),
freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:261),
freemarker.template.Configuration.getTemplate(Configuration.java:1786),
freemarker.core.Environment.getTemplateForInclusion(Environment.java:2044),
freemarker.core.Include.accept(Include.java:158),
freemarker.core.Environment.visit(Environment.java:324),
freemarker.core.Environment.process(Environment.java:302),
de.vodafone.top.render.FreemarkerPageResult.flush(FreemarkerPageResult.java:194),
de.vodafone.top.render.servlets.PageAbsolute.processRequest(PageAbsolute.java:104),
de.vodafone.top.render.servlets.PageAbsolute.doGet(PageAbsolute.java:69),
{quote}

The second "process" failed with "already closed" on:

{quote}
freemarker.cache.TemplateCache.loadTemplate(TemplateCache.java:497),
freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:409),
freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:261),
freemarker.template.Configuration.getTemplate(Configuration.java:1786),
freemarker.core.Environment.getTemplateForInclusion(Environment.java:2044),
freemarker.core.Include.accept(Include.java:158),
freemarker.core.Environment.visit(Environment.java:324),
freemarker.core.Environment.process(Environment.java:302),
{quote}

We also extended TemplateLoader with our own, returning an empty StringReader on first "process".

{quote}
  @Override
        public Reader getReader(final Object ref, final String encoding) throws IOException
{
            if (preprocessing.get()) {
                return StringReader("");
            }
            try {
                return ((FreemarkerIncludeRef) ref).getReader();
            } catch (final ExecutionException | InterruptedException e) {
                throw new IOException(e);
            }
        }
{quote}

This one got closed twice. Why? I have no idea yet.

We now extend StringReader in that way

{quote}
 @Override
        public void close() {
            if (!preprocessing.get()) {
                super.close();
            }
        }
{quote}

which solves the issue.

> Stream closed Exception
> -----------------------
>
>                 Key: FREEMARKER-29
>                 URL: https://issues.apache.org/jira/browse/FREEMARKER-29
>             Project: Apache Freemarker
>          Issue Type: Bug
>          Components: engine
>    Affects Versions: 2.3.23
>         Environment: Tomcat
>            Reporter: Christian Senkowski
>
> Hi,
> I'm getting Stream closed -exceptions flooding my log since 2.3.23 when using <#include>
> {quote}
> freemarker.core._MiscTemplateException: [... Exception message was already printed; see
it above ...]
>         at freemarker.core.Include.accept(Include.java:160)
>         at freemarker.core.Environment.visit(Environment.java:324)
>         at freemarker.core.Environment.process(Environment.java:302)
> […]
> Caused by: java.io.IOException: Stream closed
>         at java.io.StringReader.ensureOpen(StringReader.java:56)
>         at java.io.StringReader.read(StringReader.java:90)
>         at java.io.BufferedReader.read1(BufferedReader.java:210)
>         at java.io.BufferedReader.read(BufferedReader.java:286)
>         at freemarker.template.Template$LineTableBuilder.read(Template.java:701)
>         at freemarker.core.SimpleCharStream.FillBuff(SimpleCharStream.java:107)
>         at freemarker.core.SimpleCharStream.readChar(SimpleCharStream.java:188)
>         at freemarker.core.SimpleCharStream.BeginToken(SimpleCharStream.java:129)
>         at freemarker.core.FMParserTokenManager.getNextToken(FMParserTokenManager.java:7190)
>         at freemarker.core.FMParser.jj_scan_token(FMParser.java:5131)
>         at freemarker.core.FMParser.jj_3_16(FMParser.java:3812)
>         at freemarker.core.FMParser.jj_2_16(FMParser.java:3549)
>         at freemarker.core.FMParser.Root(FMParser.java:3427)
>         at freemarker.template.Template.<init>(Template.java:208)
>         at freemarker.cache.TemplateCache.loadTemplate(TemplateCache.java:495)
>         at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:409)
>         at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:261)
>         at freemarker.template.Configuration.getTemplate(Configuration.java:1786)
>         at freemarker.core.Environment.getTemplateForInclusion(Environment.java:2044)
>         at freemarker.core.Include.accept(Include.java:158)
>         ... 31 more
> {quote}
> I am extending the Freemarker Config
> {quote}
>  public static class FreemarkerConfig extends Configuration {
>         public FreemarkerConfig() {
>             super(FREEMARKER_VERSION);
>             setObjectWrapper(new DefaultObjectWrapper(FREEMARKER_VERSION));
>             setCacheStorage(new FreemarkerCacheStorage());
>             setTemplateExceptionHandler(new FreemarkerTemplateExceptionHandler());
>             setTemplateUpdateDelayMilliseconds(Integer.MAX_VALUE);
>             setOutputEncoding("utf-8");
>             setLocalizedLookup(false);
>             // create static stuff to access utility class in freemarker
>             final BeansWrapper wrapper = new BeansWrapperBuilder(FREEMARKER_VERSION).build();
>             TemplateHashModel staticModels = wrapper.getStaticModels();
>             try {
>                 TemplateHashModel util = (TemplateHashModel) staticModels.get(Util.class.getName());
>                 setSharedVariable("util", util);
>             } catch (final TemplateModelException e) {
>                 throw new IllegalStateException(e);
>             }
>         }
>     }
> {quote}
> If you need any further informations please let me know. Any help highly appreciated.
Changing everything back to 2.3.20 let this exception disappear.
> Thanks,
> Diceme7



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message