Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template.render() leaving Executor threads active #53

Closed
mehtasankets opened this issue Aug 10, 2017 · 10 comments
Closed

Template.render() leaving Executor threads active #53

mehtasankets opened this issue Aug 10, 2017 · 10 comments

Comments

@mehtasankets
Copy link

Template.render() method is creating a separate thread which is not getting closed explicitly. Method should ideally call executorService.shutdown() explicitly to properly close the thread.

Not closing threads explicitly is causing multiple threads to be alive in the system and causes problems at the time of closing the process.

@bkiers
Copy link
Owner

bkiers commented Aug 12, 2017

Thanks @mehtasankets!

@bkiers bkiers closed this as completed Aug 12, 2017
@mehtasankets
Copy link
Author

Welcom @bkiers .. Can we have new version published with this change?
Also, per my understanding this is a temporary fix and ideally the method should accept ExecutorService so that thread pool and thread management can be pushed back to the client using the library instead of library creating new threads on its own. Any thoughts?

@bkiers
Copy link
Owner

bkiers commented Aug 15, 2017

@mehtasankets ah, yeah, that would be nice. How about something like this: bf4a522 (on a separate branch for now)

@mehtasankets
Copy link
Author

@bkiers yes, above fix looks good to me..
Would you be able to publish a new jar with that change?

@bkiers
Copy link
Owner

bkiers commented Jan 18, 2018

The current release, 0.7.2, already has the shutdown of the exe-service:

    public String render(final Map<String, Object> variables) {

        if (this.templateSize > this.protectionSettings.maxTemplateSizeBytes) {
            throw new RuntimeException("template exceeds " + this.protectionSettings.maxTemplateSizeBytes + " bytes");
        }

        final LiquidWalker walker = new LiquidWalker(new CommonTreeNodeStream(root), this.tags, this.filters, this.parseSettings.flavor);

        ExecutorService executorService = Executors.newSingleThreadExecutor();

        Callable<String> task = new Callable<String>() {
            public String call() throws Exception {
                try {
                    LNode node = walker.walk();
                    Object rendered = node.render(new TemplateContext(protectionSettings, renderSettings, parseSettings.flavor, variables));
                    return rendered == null ? "" : String.valueOf(rendered);
                }
                catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        };

        Future<String> future = executorService.submit(task);
        try {
            return future.get(this.protectionSettings.maxRenderTimeMillis, TimeUnit.MILLISECONDS);
        }
        catch (TimeoutException e) {
            throw new RuntimeException("exceeded the max amount of time (" +
                    this.protectionSettings.maxRenderTimeMillis + " ms.)");
        }
        catch (Throwable t) {
            throw new RuntimeException("Oops, something unexpected happened: ", t);
        }
        finally {
            executorService.shutdown();
        }
    }

Or do you want a new release of master?

@mehtasankets
Copy link
Author

I would actually need a new version with
Future<String> future = executorService.submit(task);
inside try-catch. My application actually broke at that line.

Would you kindly provide a new jar?

@bkiers
Copy link
Owner

bkiers commented Jan 18, 2018

If you only need the JAR, you can assemble it yourself by doing a mvn clean install in the root of the project (it will be located here: ./target/liqp-0.7.3-SNAPSHOT.jar).

If you want a new version in in Maven Central, I'll need to ask someone else.

@mehtasankets
Copy link
Author

I would need it via maven central as I have different environments where I run my program and otherwise I'll have to install it everywhere.

@bkiers
Copy link
Owner

bkiers commented Jan 18, 2018

OK, I've asked for a new release to Maven Central: #72

@mosabua
Copy link
Collaborator

mosabua commented Jan 18, 2018

http://repo1.maven.org/maven2/nl/big-o/liqp/0.7.3/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants