grailsgrails3grails-2.5

Grails 2.5.1 application sporadically loses context root


We have a handful of applications deploying to the same tomcat server (currently working on upgrading to grails 3, so this may be OBE in the next few months, but it's been plaguing us for quite some time now) and two of the applications will occasionally lose their relative context root path.

Let's say we have "app1" and "app2" which deploy to server:port/app1 and server:port/app2.

app1 works just fine, but app2 will SOMETIMES (~20% of the time, maybe) deploy and all <g:link/> links (or any other generated links, such as asset locations) generate relative to the server root... the application is correctly deployed under /app2, so the links point to bad locations.

E.g., <g:link controller='hello' action='index'/> will generate the link as /hello/index rather than /app2/hello/index.

I don't know what the relevant code to post is, we've compared this to our other applications and have found nothing noticeably different in the two that are exhibiting this behavior. But it's only these two (out of a dozen) applications that ever break in this manner.

Any ideas on what could be causing this or where to look would be most appreciated.

Edit: Plugins in use:

compile "org.springframework.boot:spring-boot-starter-logging"
compile "org.springframework.boot:spring-boot-autoconfigure"
compile "org.grails:grails-core"
compile "org.springframework.boot:spring-boot-starter-actuator"
provided "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.grails:grails-dependencies"
compile "org.grails:grails-web-boot"
compile 'org.grails.plugins:cache:4.0.0.M2'
compile 'org.grails.plugins:cache-ehcache:3.0.0.M1'
compile "org.grails.plugins:scaffolding"
compile "org.grails.plugins:hibernate4"
compile "org.hibernate:hibernate-core:4.3.10.Final"
compile "org.hibernate:hibernate-ehcache:4.3.10.Final"
console "org.grails:grails-console"
profile "org.grails.profiles:web"
runtime "com.bertramlabs.plugins:asset-pipeline-grails:2.14.1"
runtime "com.h2database:h2"
testCompile "org.grails:grails-plugin-testing"
testCompile "org.grails.plugins:geb"
testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"

Solution

  • Solved ('ish)

    We finally discovered the cause of this. grailsLinkGenerator (DefaultLinkGenerator class) is either losing contextPath or it is not getting set correctly on startup. This causes all links generated using the link generator singleton (which is most, but apparently not all) to generate at server root.

    We're still working on timing to figure out if this can be run in BootStrap and deferred until the server startup is complete, but for now, as a workaround, we've added an unprotected controller action to reset this, which appears to resolve the issue (until the next time the server is restarted).

    def resetContextPath() {
      grailsLinkGenerator.contextPath = grailsApplication.config.getProperty("server.contextPath")
    
      if (grailsLinkGenerator instanceof CachingLinkGenerator) {
        grailsLinkGenerator.clearCache()
      }
    }
    

    Still no idea why it only occurs on some apps on some servers (probably timing of applications starting), but this at least lets us fix the issue without requiring a restart.