[MNG-7487] Fix deadlock during forked lifecycle executions

# Conflicts:
#	maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
This commit is contained in:
Guillaume Nodet 2022-05-12 10:04:51 +02:00
parent aec341a7a9
commit 0b0a96782e
1 changed files with 23 additions and 14 deletions

View File

@ -92,6 +92,8 @@ public class MojoExecutor
@Requirement
private PlexusContainer container;
private final Map<Thread, MojoDescriptor> mojos = new ConcurrentHashMap<>();
public MojoExecutor()
{
}
@ -228,10 +230,7 @@ public class MojoExecutor
}
}
try ( ProjectLock lock = new ProjectLock( session, mojoDescriptor, aggregatorLock ) )
{
doExecute( session, mojoExecution, projectIndex, dependencyContext );
}
doExecute( session, mojoExecution, projectIndex, dependencyContext );
}
/**
@ -242,13 +241,14 @@ public class MojoExecutor
* TODO: ideally, the builder should take care of the ordering in a smarter way
* TODO: and concurrency issues fixed with MNG-7157
*/
private static class ProjectLock implements AutoCloseable
private class ProjectLock implements AutoCloseable
{
final Lock acquiredAggregatorLock;
final Lock acquiredProjectLock;
ProjectLock( MavenSession session, MojoDescriptor mojoDescriptor, ReadWriteLock aggregatorLock )
ProjectLock( MavenSession session, MojoDescriptor mojoDescriptor )
{
mojos.put( Thread.currentThread(), mojoDescriptor );
if ( session.getRequest().getDegreeOfConcurrency() > 1 )
{
boolean aggregator = mojoDescriptor.isAggregator();
@ -276,6 +276,7 @@ public class MojoExecutor
{
acquiredAggregatorLock.unlock();
}
mojos.remove( Thread.currentThread() );
}
@SuppressWarnings( { "unchecked", "rawtypes" } )
@ -314,8 +315,23 @@ public class MojoExecutor
ensureDependenciesAreResolved( mojoDescriptor, session, dependencyContext );
eventCatapult.fire( ExecutionEvent.Type.MojoStarted, session, mojoExecution );
try ( ProjectLock lock = new ProjectLock( session, mojoDescriptor ) )
{
doExecute2( session, mojoExecution );
}
finally
{
for ( MavenProject forkedProject : forkedProjects )
{
forkedProject.setExecutionProject( null );
}
}
}
private void doExecute2( MavenSession session, MojoExecution mojoExecution )
throws LifecycleExecutionException
{
eventCatapult.fire( ExecutionEvent.Type.MojoStarted, session, mojoExecution );
try
{
try
@ -336,13 +352,6 @@ public class MojoExecutor
throw e;
}
finally
{
for ( MavenProject forkedProject : forkedProjects )
{
forkedProject.setExecutionProject( null );
}
}
}
public void ensureDependenciesAreResolved( MojoDescriptor mojoDescriptor, MavenSession session,