jknack / handlebars.java

Logic-less and semantic Mustache templates with Java
http://jknack.github.io/handlebars.java
Other
1.48k stars 383 forks source link

URLConnection may cause file handle leaks #393

Closed plesiecki closed 9 years ago

plesiecki commented 9 years ago

Hello!

Recently I that discovered method URLTemplateSource.lastModified may cause file handle leaks. It happens when you run your application from its pacakaged jar and handlebars reads last modification date from resource with URL like jar:file:/path/app.jar!/template.hbs.

plesiecki commented 9 years ago

I would like to propose a pull request for this issue.

jknack commented 9 years ago

I will look at the pull later, but don't think there is a leak. Everything is closed it: https://github.com/jknack/handlebars.java/blob/master/handlebars/src/main/java/com/github/jknack/handlebars/io/URLTemplateSource.java#L125-131

plesiecki commented 9 years ago

I'm pretty sure there is ;)

Some references: http://www.genevski.com/2010/04/javaneturl-and-jarurlconnection-may.html http://webcache.googleusercontent.com/search?q=cache:_sNS9oOvDnwJ:sourceforge.net/p/freemarker/bugs/189/+&cd=7&hl=pl&ct=clnk&gl=pl

Also https://github.com/kohsuke/file-leak-detector maybe useful for debugging.

plesiecki commented 9 years ago

Here goes steps to reproduce:

  1. git clone https://github.com/plesiecki/handlebars-spring-boot-starter-example.git && cd handlebars-spring-boot-starter-example
  2. Run ./gradlew installapp && build/install/handlebars-spring-boot-starter-example/bin/handlebars-spring-boot-starter-example
  3. In another console run while true; do lsof -p $(jps | grep App | cut -d ' ' -f 1) | wc -l ; sleep 1; done this is number of open files by app from step 2
  4. curl locahost:8080/ multiple times
  5. Watch counter from step 3. It will increase by 35 each time you open localhost:8080/ - 35 it's a number of inclusion of handlebars template https://github.com/plesiecki/handlebars-spring-boot-starter-example/blob/master/src/main/resources/templates/hello.hbs#L8-L43.
jknack commented 9 years ago

odd, but thanks!

jknack commented 9 years ago

just realize a have the same bug in another (and awesome) project: https://github.com/jooby-project/jooby https://github.com/jooby-project/jooby/blob/master/jooby/src/main/java/org/jooby/internal/URLAsset.java :S

thanks again.

plesiecki commented 9 years ago

Glad I could help. Any idea when we can expect the next release?

BTW Jooby looks interesting ;)

jknack commented 9 years ago

will push a release now (will be available in 2/3 hours)

plesiecki commented 9 years ago

Cool! I've updated my https://github.com/allegro/handlebars-spring-boot-starter ;)