function dctFinishInteractiveSession
; %#ok Undocumented

% Copyright 2006-2020 The MathWorks, Inc.

% This function should be called to finish any interactive session
% that has been started by a parallel job that has pIsInteractiveJob
% true. The behaviour is to pick up the saved post functions that 
% would have normally run in a non-interactive job and run them now.

root = distcomp.getdistcompobjectroot;

try
    % Make sure that the session has the correct MPI functions to terminate 
    dctSchedulerMessage(5, 'About to reset MPI functions.');
    parallel.internal.mpi.registerFunctions('mwmpi');
    dctSchedulerMessage(5, 'About to run stored functions.');
    dctStoreFunctionArray('run');
catch e
    if isa(e, 'distcomp.ExitException')
        m = message( 'parallel:cluster:UnexpectedExitFailure', e.message );
        root.CurrentErrorHandlers.errorFcn( e.CauseException, m.getString() );
    else
        m = message( 'parallel:cluster:UnexpectedExitFailureNoRestart' );
        root.CurrentErrorHandlers.errorFcn( e, m.getString() );
    end
end
dctSchedulerMessage(2, 'Unexpectedly completed dctFinishInteractiveSession.');
