Bug 42503 - ServletContext.getResourceAsStream returns stale data
Summary: ServletContext.getResourceAsStream returns stale data
Status: RESOLVED FIXED
Alias: None
Product: Tomcat 6
Classification: Unclassified
Component: Catalina (show other bugs)
Version: unspecified
Hardware: Other other
: P3 normal (vote)
Target Milestone: default
Assignee: Tomcat Developers Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-05-23 16:52 UTC by Arvind Srinivasan
Modified: 2007-12-30 14:06 UTC (History)
0 users



Attachments
Data file for the test case (6 bytes, text/plain)
2007-05-23 16:53 UTC, Arvind Srinivasan
Details
Test case (250 bytes, text/plain)
2007-05-23 16:54 UTC, Arvind Srinivasan
Details
Patch that updates the last modified time of the file when it is inserted into the cache (1.37 KB, patch)
2007-05-23 17:00 UTC, Arvind Srinivasan
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Arvind Srinivasan 2007-05-23 16:52:35 UTC
Under certain conditions, the resource cache implementation used by the
servlet/JSP engine (in org.apache.naming.resource) does not detect that a file
in its cache has been modified and therefore returns the previous contents of
the file.

This happens only 
 * when the file contents are modified but the file length doesn't change
 * until the time the first change in the file is detected after server startup
   
To reproduce the problem, put the attached test.jsp and test.txt into the ROOT
web app and then do the following. (I used Tomcat 6's trunk for the test)

% cat test.txt
abcd
% telnet . 9080 
Trying 0.0.0.0...
Connected to ..
Escape character is '^]'.
GET /test.jsp HTTP/1.0

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=706D6FE8B06687509D42E79DBEE266EF; Path=/
Content-Type: text/html
Content-Length: 26
Date: Wed, 23 May 2007 23:03:05 GMT
Connection: close



File contents=[
abcd

]
Connection to . closed by foreign host.

% cat test.txt (change the contents but not the size)
efgh
 % telnet . 9080 
Trying 0.0.0.0...
Connected to ..
Escape character is '^]'.
GET /test.jsp HTTP/1.0

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=2B144247B747F54184114FA82869DE14; Path=/
Content-Type: text/html
Content-Length: 26
Date: Wed, 23 May 2007 23:04:24 GMT
Connection: close



File contents=[
abcd

]
Comment 1 Arvind Srinivasan 2007-05-23 16:53:36 UTC
Created attachment 20263 [details]
Data file for the test case

Put test.txt in the ROOT web app
Comment 2 Arvind Srinivasan 2007-05-23 16:54:14 UTC
Created attachment 20264 [details]
Test case

Put test.jsp in the ROOT web app.
Comment 3 Arvind Srinivasan 2007-05-23 17:00:19 UTC
Created attachment 20265 [details]
Patch that updates the last modified time of the file when it is inserted into the cache

The problem occurs because the last modified timestamp in the
FileResourceAttribute of CacheEntry isn't updated at the time the CacheEntry is
first inserted into the cache. The timestamp gets updated when
ProxyDirContext.revalidate() is executed. The window between when the
CacheEntry was inserted and when ProxyDirContext.revalidate() is called is
where the problem occurs.

The attached patch fixes this by updating the last modified (and creation)
timestamp at the time the resource is created (i.e. inserted into the cache).
Comment 4 Tim Funk 2007-12-26 17:59:28 UTC
patch applied to trunk
pending vote for 6.0.x
Comment 5 Mark Thomas 2007-12-30 14:06:06 UTC
The vote passed and the patch has been applied. It will be included in 6.0.16
onwards.