Skip to content

Embedded Tomcat container binds port before application is completely started #55

@nebhale

Description

@nebhale

Currently, when an application is starting up, the embedded Tomcat container binds to its port before the application has finished starting. To see this in action, attempt to start a controller that sleeps in a static block. Something like the following:

Controller
class Example {

    static {
        Thread.sleep(30*1000)
    }

    @RequestMapping("/")
    @ResponseBody
    public String controller(HttpEntity<String> requestEntity) {
        return "Hello World!"
    }
}

When you run spring run app.groovy, you'll see the console stall before the application has stated completely. However, if you attempt to curl http://localhost:8080, curl will also stall because the port is bound and while no response has been returned, a socket connection can be completed. An alternative test is to telnet localhost 8080 and see if a connection can be made.

This is problematic in Cloud Foundry as the HealthManager determines when an application has started by attempted a socket connection to it's provided port. Whether the application is running behind the scenes isn't actually checked, so a false-positive started can be returned.

The intended behavior should be that when the console is stalled as described above, curl should return curl: (7) couldn't connect to host indicating that the port hasn't even been bound to yet. This is typically set in the Tomcat configuration using bindOnInit. I believe that there is a programmatic equivalent to this configuration parameter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions