Skip to content

Commit f0eb04e

Browse files
committed
Properly kill child processes when canceling.
1 parent 04160f7 commit f0eb04e

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

src/main/java/fiji/plugin/trackmate/cellpose/CellposeDetector.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
* it under the terms of the GNU General Public License as
99
* published by the Free Software Foundation, either version 3 of the
1010
* License, or (at your option) any later version.
11-
*
11+
*
1212
* This program is distributed in the hope that it will be useful,
1313
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1414
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1515
* GNU General Public License for more details.
16-
*
16+
*
1717
* You should have received a copy of the GNU General Public
1818
* License along with this program. If not, see
1919
* <http://www.gnu.org/licenses/gpl-3.0.html>.
@@ -181,19 +181,27 @@ public boolean process()
181181
* factor, which is to be expected)
182182
*/
183183
if ( !cli.useGPU().getValue() && IJ.isMacintosh() )
184+
{
184185
nConcurrentTasks = numThreads;
186+
}
185187
else
188+
{
186189
nConcurrentTasks = 1;
190+
}
187191

188192
final List< List< ImagePlus > > timepoints = new ArrayList<>( nConcurrentTasks );
189193
for ( int i = 0; i < nConcurrentTasks; i++ )
194+
{
190195
timepoints.add( new ArrayList<>() );
196+
}
191197

192198
Iterator< List< ImagePlus > > it = timepoints.iterator();
193199
for ( int t = 0; t < imps.size(); t++ )
194200
{
195201
if ( !it.hasNext() )
202+
{
196203
it = timepoints.iterator();
204+
}
197205
it.next().add( imps.get( t ) );
198206
}
199207

@@ -203,7 +211,9 @@ public boolean process()
203211

204212
processes.clear();
205213
for ( final List< ImagePlus > list : timepoints )
214+
{
206215
processes.add( new CellposeTask( list ) );
216+
}
207217

208218
/*
209219
* Pass tasks to executors.
@@ -224,7 +234,9 @@ public boolean process()
224234
{
225235
results = executors.invokeAll( processes );
226236
for ( final Future< String > future : results )
237+
{
227238
resultDirs.add( future.get() );
239+
}
228240
}
229241
catch ( final InterruptedException | ExecutionException e )
230242
{
@@ -248,7 +260,9 @@ public boolean process()
248260
for ( final CellposeTask task : processes )
249261
{
250262
if ( !task.isOk() )
263+
{
251264
return false;
265+
}
252266
}
253267

254268
/*
@@ -273,9 +287,13 @@ public boolean process()
273287
if ( tpImp.getType() != ImagePlus.GRAY16 )
274288
{
275289
if ( nslices > 1 )
290+
{
276291
new StackConverter( tpImp ).convertToGray16();
292+
}
277293
else
294+
{
278295
new ImageConverter( tpImp ).convertToGray16();
296+
}
279297
}
280298
break;
281299
}
@@ -411,7 +429,9 @@ public void cancel( final String reason )
411429
isCanceled = true;
412430
cancelReason = reason;
413431
for ( final CellposeTask task : processes )
432+
{
414433
task.cancel();
434+
}
415435
}
416436

417437
@Override
@@ -465,7 +485,10 @@ public boolean isOk()
465485
void cancel()
466486
{
467487
if ( process != null )
468-
process.destroy();
488+
{
489+
process.toHandle().descendants().forEach( ProcessHandle::destroyForcibly );
490+
process.destroyForcibly();
491+
}
469492
}
470493

471494
@Override

src/main/java/fiji/plugin/trackmate/cellpose/sam/CellposeSAMDetector.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,27 @@ public boolean process()
159159

160160
final int nConcurrentTasks;
161161
if ( !cli.useGPU().getValue() && IJ.isMacintosh() )
162+
{
162163
nConcurrentTasks = numThreads;
164+
}
163165
else
166+
{
164167
nConcurrentTasks = 1;
168+
}
165169

166170
final List< List< ImagePlus > > timepoints = new ArrayList<>( nConcurrentTasks );
167171
for ( int i = 0; i < nConcurrentTasks; i++ )
172+
{
168173
timepoints.add( new ArrayList<>() );
174+
}
169175

170176
Iterator< List< ImagePlus > > it = timepoints.iterator();
171177
for ( int t = 0; t < imps.size(); t++ )
172178
{
173179
if ( !it.hasNext() )
180+
{
174181
it = timepoints.iterator();
182+
}
175183
it.next().add( imps.get( t ) );
176184
}
177185

@@ -181,7 +189,9 @@ public boolean process()
181189

182190
processes.clear();
183191
for ( final List< ImagePlus > list : timepoints )
192+
{
184193
processes.add( new CellposeTask( list ) );
194+
}
185195

186196
/*
187197
* Pass tasks to executors.
@@ -202,7 +212,9 @@ public boolean process()
202212
{
203213
results = executors.invokeAll( processes );
204214
for ( final Future< String > future : results )
215+
{
205216
resultDirs.add( future.get() );
217+
}
206218
}
207219
catch ( final InterruptedException | ExecutionException e )
208220
{
@@ -226,7 +238,9 @@ public boolean process()
226238
for ( final CellposeTask task : processes )
227239
{
228240
if ( !task.isOk() )
241+
{
229242
return false;
243+
}
230244
}
231245

232246
/*
@@ -251,9 +265,13 @@ public boolean process()
251265
if ( tpImp.getType() != ImagePlus.GRAY16 )
252266
{
253267
if ( nslices > 1 )
268+
{
254269
new StackConverter( tpImp ).convertToGray16();
270+
}
255271
else
272+
{
256273
new ImageConverter( tpImp ).convertToGray16();
274+
}
257275
}
258276
break;
259277
}
@@ -389,7 +407,9 @@ public void cancel( final String reason )
389407
isCanceled = true;
390408
cancelReason = reason;
391409
for ( final CellposeTask task : processes )
410+
{
392411
task.cancel();
412+
}
393413
}
394414

395415
@Override
@@ -443,7 +463,10 @@ public boolean isOk()
443463
void cancel()
444464
{
445465
if ( process != null )
446-
process.destroy();
466+
{
467+
process.toHandle().descendants().forEach( ProcessHandle::destroyForcibly );
468+
process.destroyForcibly();
469+
}
447470
}
448471

449472
@Override
@@ -480,9 +503,13 @@ public String call() throws Exception
480503
final int c = Integer.parseInt( cStr );
481504
final ImagePlus chanImp;
482505
if ( c <= 0 )
506+
{
483507
chanImp = imp; // all channels
508+
}
484509
else
510+
{
485511
chanImp = new Duplicator().run( imp, c, c, 0, 0, 0, 0 );
512+
}
486513
IJ.saveAsTiff( chanImp, Paths.get( tmpDir.toString(), name ).toString() );
487514
}
488515

0 commit comments

Comments
 (0)