Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 125524

Summary: PublishUtil.createZipFile() has problem deleting file
Product: [WebTools] WTP ServerTools Reporter: Kathy Chan <kathy>
Component: jst.serverAssignee: Tim deBoer <deboer>
Status: CLOSED FIXED QA Contact:
Severity: major    
Priority: P2 CC: jeffliu, larryisaacs, thatnitind
Version: 1.0.1   
Target Milestone: 1.5.1 M151   
Hardware: PC   
OS: Windows XP   
See Also: https://git.eclipse.org/r/107802
https://git.eclipse.org/r/107938
Whiteboard:
Attachments:
Description Flags
Patch to rewrite files during publish if they can't be deleted none

Description Kathy Chan CLA 2006-01-27 13:29:23 EST
Driver:  M 0124_0254 + applied patch for jst.cons.ui in bug 117930

Scenario:

- Create a Web service client. Select to "test Web service"
- You can use any WSDL file or this URL:
http://www.xmethods.net/sd/2001/TemperatureService.wsdl
- On the 3rd page, type in a Java project (say "j4"), select "Java Utility project".

The wizard would create a Web project "j4Sample" and put j4.jar in the WEB-INF/lib directory.

The scenario is fine the first time.  However, if I repeat on a started server a second time, I would get:


Publishing failed
  Error creating zip file j4.jar: Error deleting D:\eclipse311\eclipse\runtime-workspace_release0127a\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\webapps\j4Sample\WEB-INF\lib\j4.jar
      java.lang.Exception: Error deleting D:\eclipse311\eclipse\runtime-workspace_release0127a\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\webapps\j4Sample\WEB-INF\lib\j4.jar
      at org.eclipse.jst.server.core.PublishUtil.createZipFile(PublishUtil.java:407)
      at org.eclipse.jst.server.tomcat.core.internal.PublishOperation2.publishJar(PublishOperation2.java:108)
      at org.eclipse.jst.server.tomcat.core.internal.PublishOperation2.execute(PublishOperation2.java:53)
      at org.eclipse.wst.server.core.model.ServerBehaviourDelegate.performTasks(ServerBehaviourDelegate.java:797)
      at org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publish(ServerBehaviourDelegate.java:586)
      at org.eclipse.wst.server.core.internal.Server.doPublish(Server.java:801)
      at org.eclipse.wst.server.core.internal.Server.publish(Server.java:789)
      at org.eclipse.jst.ws.internal.consumption.ui.command.StartServerCommand$1.run(StartServerCommand.java:129)
      at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:113)

I checked and see that the j4.jar is not in the workspace but in the .metadata directory.  If I try to delete it, it would say another process is using the file.

I tried a few other times and sometimes got:

Publishing failed
  Error creating zip file j1.jar: Error deleting D:\eclipse311\eclipse\runtime-workspace_release0127a\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\webapps\j1Sample\WEB-INF\lib\j1.jar
  Error creating zip file j2.jar: ZIP file must have at least one entry

Please let me know if you need help reproducing the problem.  We don't want to check in the fix for 117930 until this can be resolved.
Comment 1 Jeffrey Liu CLA 2006-02-09 10:21:22 EST
Added to the 1.5 hot list. Setting priority to P2.
Comment 2 David Williams CLA 2006-03-02 14:43:31 EST
assuming will be addressed next milestone. If I'm mistaken, please correct. 
Comment 3 Jeffrey Liu CLA 2006-03-09 14:40:03 EST
ETA: Will work on it over the next couple weeks.
Comment 4 Tim deBoer CLA 2006-03-09 14:41:08 EST
Will be addressed by "friendly publishing", where we'll try multiple times to do the publish and will give a better error message if it still fails. There is a chance this will still cause the problem (e.g. if the server has locked the file and never lets go) but it will solve problems with temporary locks and will provide better feedback.
Comment 5 Jeffrey Liu CLA 2006-04-21 17:58:15 EDT
Tim, any updates on this hot bug?
Comment 6 Tim deBoer CLA 2006-04-28 00:19:46 EDT
Fix checked into HEAD. Will release for 1.5 once I-build is declared.
Comment 7 Tim deBoer CLA 2006-04-28 00:21:27 EDT
*** Bug 120709 has been marked as a duplicate of this bug. ***
Comment 8 Jeffrey Liu CLA 2006-04-28 11:26:08 EDT
Update all 1.5 hot bugs to use the new hot bug process.
Comment 9 Tim deBoer CLA 2006-05-01 00:28:49 EDT
Released to 1.5 stream.
Comment 10 Kathy Chan CLA 2006-06-15 10:50:21 EDT
I retried the scenario with WTP 1.5 06150639 driver and a similar problem is still there.  Here's the trace (note that "u1" is the name of the utility Jar the Web project is dependent on):

Publishing failed
  Error creating zip file u1.jar: Could not delete previous copy, which may be locked by another process.
      java.lang.Exception: Could not delete previous copy, which may be locked by another process.
      at org.eclipse.jst.server.core.PublishUtil.moveTempFile(PublishUtil.java:476)
      at org.eclipse.jst.server.core.PublishUtil.createZipFile(PublishUtil.java:407)
      at org.eclipse.jst.server.tomcat.core.internal.PublishOperation2.publishJar(PublishOperation2.java:108)
      at org.eclipse.jst.server.tomcat.core.internal.PublishOperation2.execute(PublishOperation2.java:53)
      at org.eclipse.wst.server.core.model.ServerBehaviourDelegate.performTasks(ServerBehaviourDelegate.java:801)
      at org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publish(ServerBehaviourDelegate.java:590)
      at org.eclipse.wst.server.core.internal.Server.doPublish(Server.java:803)
      at org.eclipse.wst.server.core.internal.Server.publish(Server.java:791)
      at org.eclipse.jst.ws.internal.consumption.ui.command.StartServerCommand$1.run(StartServerCommand.java:129)
      at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:113)
Comment 11 Tim deBoer CLA 2006-06-15 10:57:12 EDT
If WTP can't delete the file, it probably means that the server is actually locking the file. If that's true, it's regular Tomcat behaviour and nothing WTP can do about it - can you show anything that would prove this is a WTP problem?
Comment 12 Larry Isaacs CLA 2006-07-31 20:32:46 EDT
I can't at the moment speak about class files, but I know for certain that Windows locks the file handle for jars while the JarFile is kept open by Tomcat's webapp classloader.  The jar can't be deleted until Tomcat is shutdown.  However, even though it can't be deleted, it *can* be rewritten.  For consideration, I'm attaching a patch that works around this issue by rewritting the jar file with the contents of the temp file if the jar can't be deleted.

This particular patch goes the whole nine yards by adding a boolean "allowRewrite" argument to more than just the jar related methods.  Old public methods are preserved with new versions added with the new argument, in case some adopter determined to use internal API might want the choice.  The patch also updates the Tomcat plug-in to specify allowRewrite true.  This fixes the publish issue for me on Windows, at least for updating utility projects used by a webapp that is published to a running Tomcat server.  I would expect it to fix this bug as well with respect to jars.
I'm not aware of the same kind of locking problem for class files, which has been mentioned in other bugs, so I'm not sure if this would help in those cases.

This patch could be scaled down to just address jar file replacement if desired.  Let me know if you want me to create a smaller version of the patch.
Comment 13 Larry Isaacs CLA 2006-07-31 20:37:50 EDT
Created attachment 47122 [details]
Patch to rewrite files during publish if they can't be deleted

This patch was developed for 1.5.1.  I haven't checked it against 2.0 yet, but I'll do so and attach another patch for 2.0 if necessary.
Comment 14 Tim deBoer CLA 2006-08-01 09:06:20 EDT
Thanks for the patch, Larry. The 2.0 stream is identical to 1.5.1 right now, so no additional work needs to be done. I'll review the patch and check the changes in.
Comment 15 Tim deBoer CLA 2006-08-09 22:12:35 EDT
I may be wrong, but I think Larry's patch is great and I can't see any reason why someone wouldn't want to use it. Therefore, I'm skipping duplicating all of the API and putting the fix directly into the main stream so that all servers will take advantage of it. I hope everyone is ok with this and I'm not missing any reason why a server wouldn't want to try this behaviour.

Fix dropped to HEAD. I can't claim we don't have any bugs or are still missing something, but at this point we try several different methods and are bending over backwards attempting to work with files locked by the server. If a publish still fails after this fix, it seems likely that the file is simply locked and it will be a limitation of the server.
Comment 16 Tim deBoer CLA 2006-08-09 22:19:01 EDT
Fix released to 1.5.1 and 2.0 streams.
Comment 17 Kathy Chan CLA 2006-09-22 11:43:45 EDT
I tried the original scenarios a few times on WTP 1.5.1 200609220504 and the problem has not surface. Thanks Tim and Larry for the fix.
Comment 18 Tim deBoer CLA 2006-09-26 13:04:06 EDT
Closing.
Comment 19 David Williams CLA 2006-09-27 09:06:26 EDT
This is part of a "mass update" where bugs showing "fixed" between 7/1/2006 and 9/27/2006 are assumed fixed in WTP 1.5.1. feel free to change the target milestone if you know a more accurate setting. 
Comment 20 Larry Isaacs CLA 2006-09-28 20:47:21 EDT
Darryl Miles raised an issue on the newsgroup about rewriting jars causing JVM problems.  I had tested the rewriting with a utility jar with multiple classes and verified that with Tomcat, changes to all classes in the jar occurred successfully.  Darryl's post reminded me that for this to succeed, automatic reloading of the web application has to occur.  I had intended to eventually set the "allowRewrite" argument in my patch based on the auto-reload setting on the web application.  This seemed too big a change for WTP 1.5.x, but I failed to mention it in comments.  Since this argument wasn't included in the released changes, at some point I'll create an enhancement request to consider adding the "allowRewrite" argument to some extent in WTP 2.0 to better address this issue.

At the moment for jars, I believe the Tomcat plug-in is the only consumer so this shouldn't be a problem in general.  However, we should be watchful concerning any new uses of jar publishing.  As Darryl points out, it's probably a bad idea to rewrite the jar in situations where there isn't good chance that stale classes will be discarded.  
Comment 21 Eclipse Genie CLA 2017-10-11 16:00:29 EDT
New Gerrit change created: https://git.eclipse.org/r/107802
Comment 22 Eclipse Genie CLA 2017-10-11 16:04:37 EDT
New Gerrit change created: https://git.eclipse.org/r/107938