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

Bug 265973

Summary: Repository settings cannot be persisted if engine is not available
Product: [Eclipse Project] Equinox Reporter: John Arthorne <john.arthorne>
Component: p2Assignee: John Arthorne <john.arthorne>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: daniel_megert, dj.houghton, jdmiles, markus.kell.r, matthew, Olivier_Thomann, pascal, pwebster, steffen.pingel
Version: unspecified   
Target Milestone: 3.6 M6   
Hardware: PC   
OS: Windows Vista   
Whiteboard:

Description John Arthorne CLA 2009-02-24 10:57:37 EST
There can be an NPE on shutdown when the agent location service is no longer available. 

java.lang.NullPointerException
	at org.eclipse.equinox.internal.p2.engine.ProfilePreferences.getDefaultLocation(ProfilePreferences.java:141)
	at org.eclipse.equinox.internal.p2.engine.ProfilePreferences.save(ProfilePreferences.java:153)
	at org.eclipse.core.internal.preferences.EclipsePreferences.flush(EclipsePreferences.java:352)
	at org.eclipse.core.internal.preferences.EclipsePreferences.flush(EclipsePreferences.java:340)
	at org.eclipse.equinox.internal.p2.core.helpers.AbstractRepositoryManager$SaveJob.run(AbstractRepositoryManager.java:57)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
Comment 1 John Arthorne CLA 2009-02-24 12:10:58 EST
Tracing through the code I can't see why this would happen. If the agent location service is null it means the p2.core bundle has been stopped. The repository bundles join the save job in their stop method, so these jobs should never be running after the p2.core bundle is stopped.
Comment 2 Steffen Pingel CLA 2009-02-25 01:06:24 EST
I am seeing the same error when running Mylyn headless tests:


!SESSION 2009-02-24 16:25:47.965 -----------------------------------------------
eclipse.buildId=I20090224-0800
java.version=1.5.0_15
java.vendor=Sun Microsystems Inc.
BootLoader constants: OS=linux, ARCH=x86, WS=gtk, NL=en_US
Framework arguments:  -application org.eclipse.test.uitestapplication formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,/home/tools/mylyn/eclipse/test-I20090224-0800/eclipse/org.eclipse.mylyn.tests.AllTests.xml -testPluginName org.eclipse.mylyn.tests -className org.eclipse.mylyn.tests.AllTests
Command-line arguments:  -application org.eclipse.test.uitestapplication -data /home/tools/mylyn/eclipse/test-I20090224-0800/eclipse/mylyn_folder formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,/home/tools/mylyn/eclipse/test-I20090224-0800/eclipse/org.eclipse.mylyn.tests.AllTests.xml -testPluginName org.eclipse.mylyn.tests -className org.eclipse.mylyn.tests.AllTests -os linux -ws gtk -arch x86 -consolelog


!ENTRY org.eclipse.core.jobs 4 2 2009-02-24 16:26:12.910
!MESSAGE An internal error occurred during: "Saving repository settings".
!STACK 0
java.lang.NullPointerException
	at org.eclipse.equinox.internal.p2.engine.ProfilePreferences.getDefaultLocation(ProfilePreferences.java:141)
	at org.eclipse.equinox.internal.p2.engine.ProfilePreferences.save(ProfilePreferences.java:153)
	at org.eclipse.core.internal.preferences.EclipsePreferences.flush(EclipsePreferences.java:352)
	at org.eclipse.core.internal.preferences.EclipsePreferences.flush(EclipsePreferences.java:340)
	at org.eclipse.equinox.internal.p2.core.helpers.AbstractRepositoryManager$SaveJob.run(AbstractRepositoryManager.java:57)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
Comment 3 Markus Keller CLA 2009-02-25 06:17:04 EST
I get the NPE twice (with I20090224-0800).
Comment 4 Dani Megert CLA 2009-02-25 07:01:45 EST
Me too. Each time I exit.
Comment 5 Pascal Rapicault CLA 2009-02-25 10:22:09 EST
I get it too.
Comment 6 John Arthorne CLA 2009-02-25 10:41:52 EST
*** Bug 266145 has been marked as a duplicate of this bug. ***
Comment 7 Olivier Thomann CLA 2009-02-25 14:58:50 EST
Same for me.
Comment 8 John Arthorne CLA 2009-02-25 17:05:20 EST
The problem is not that the p2.core bundle has been stopped, but that the engine bundle has been stopped:

->stopped org.eclipse.equinox.p2.engine_1.0.100.qualifier [262]
->stopped org.eclipse.equinox.p2.artifact.repository_1.0.100.qualifier [260]
Trying to suspend bundle org.eclipse.equinox.p2.metadata.repository_1.0.100.qualifier [264]

!ENTRY org.eclipse.equinox.p2.engine 2 0 2009-02-25 16:49:54.546
!MESSAGE Agent location service not available
!STACK 0
java.lang.RuntimeException
	at org.eclipse.equinox.internal.p2.engine.ProfilePreferences.getDefaultLocation(ProfilePreferences.java:147)

(I have replaced the NPE with a logged RuntimeException here).

The repository managers cannot save their state after the engine bundle has been stopped. There is a small window between the engine stopping and repositories stopping where any changes to the set of repositories cannot be persisted.
Comment 9 John Arthorne CLA 2009-02-26 11:58:16 EST
*** Bug 266340 has been marked as a duplicate of this bug. ***
Comment 10 Olivier Thomann CLA 2009-02-27 09:06:00 EST
This can also be found inside the test framework.
From the console log of the api plugin tests suite:
!ENTRY org.eclipse.core.jobs 4 2 2009-02-27 02:24:45.875
!MESSAGE An internal error occurred during: "Saving repository settings".
!STACK 0
java.lang.NullPointerException
	at org.eclipse.equinox.internal.p2.engine.ProfilePreferences.getDefaultLocation(ProfilePreferences.java:141)
	at org.eclipse.equinox.internal.p2.engine.ProfilePreferences.save(ProfilePreferences.java:153)
	at org.eclipse.core.internal.preferences.EclipsePreferences.flush(EclipsePreferences.java:352)
	at org.eclipse.core.internal.preferences.EclipsePreferences.flush(EclipsePreferences.java:340)
	at org.eclipse.equinox.internal.p2.core.helpers.AbstractRepositoryManager$SaveJob.run(AbstractRepositoryManager.java:57)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
Comment 11 John Arthorne CLA 2009-02-28 15:52:52 EST
The root problem here is attempting to persist repository settings after the engine bundle has stopped. I have released a number of changes to mitigate this:

 - The repository manager previously saved preferences in a job. This increased the gap of time between repository manager changes and calling the engine, and if the engine bundle stopped during that gap the problem would occur. The fix for bug 266205 removed this job from the repository manager, so the engine is invoked immediately when there are repository changes.
 - There is now a job at the engine level that is responsible for persisting profile preferences. This job is joined in the Engine activator's stop() method, to ensure all changes are flushed prior to shutdown
 - There was one case where we didn't immediately persist changes to the repository manager (bug 266382). As a result, the change would only be persisted at repository bundle stop() time, which is guaranteed to fail because the engine is not available at this point.
 - If there are unsaved repository changes when the repository manager is shutting down, a warning is logged rather than throwing an exception.

I'm leaving this bug open for now because there are still two possible problems:
 - If there are any other repository changes that are not persisted immediately, then the persistence on shutdown will attempt to save them and fail. The logging I added will help us find if there are more such cases
 - If someone changes repository settings after the engine bundle has shut down, they cannot be persisted. Again the logging I added will help us find out if this is a problem.
Comment 12 John Arthorne CLA 2009-02-28 15:55:24 EST
I'm renaming bug and clearing M5 target. The "root" problem of shutdown order is difficult to solve. We would need to have the repository bundles depend on the engine bundle, so that shutdown order is reversed. That is not currently possible but future restructuring of the p2 bundles may help to sort this out. 

The other potential fix is to persist repository changes in the configuration area in the case where the engine is not available. On startup we would merge the two sets of preferences. This is a bit ugly/complex, but would also handle cases where there is no engine bundle present (for example in the mirroring app).
Comment 13 John Arthorne CLA 2010-02-11 10:36:33 EST
*** Bug 302591 has been marked as a duplicate of this bug. ***
Comment 14 John Arthorne CLA 2010-02-11 15:49:42 EST
I have released a fix for this in HEAD, by making use of the agent lifecycle methods added in bug 300450.
Comment 15 John Arthorne CLA 2010-02-17 14:26:12 EST
This fix was released.