# How to implement LinkLaunch-like functionality with timeout?

tl;dr: What is the best way (and correct sequence of operations) to connect two programs using MathLink while anticipating failures / timeouts?

### Launch program and connect, with timeout

I have a program P that I want to connect to a Mathematica kernel K through MathLink/WSTP. P may take a long time to launch, and it may also fail completely (depending on the computer where it is being run).

The expected behaviour is this:

• K starts up P, and waits for a connection.
• If P does not connect back within time t, K should fail gracefully and report an error.
• If P took longer to start than K expected, and at that point it can no longer connect, it should also fail gracefully.
• They should not leave any broken links behind if the connection process fails. Preferably, no shared memory link names should become unusable in case of failure.

K will use Wolfram Language functions. P will use the C MathLink API.

I believe I should rely on LinkReadyQ for the timeout (see its second argument), but it is not clear what is the correct sequence of operations to perform if robustness is critical.

It is also not clear to me which of K and P should use the Connect and Listen link modes (I think K should use Listen and P should use Connect).

### Connect two already running programs

I would also like to implement a second mode of operation for K and P:

Suppose that both K and P are already running. I would like to implement a user-friendly method to connect them together. Ideally, the user would issue a command both in K and P:

connectToP[] (* Issued in K. *)

connectToK[] (* Issued in P.  P is not Mathematica, but for this example, it's okay to pretend it is. *)


Preferably, the order in which these commands are issued should not matter, and the connection would be set up automatically. This is for the sake of user-friendliness. Again, it is preferable to make the commands abortable without bad side effects (or to allow them to time out).

Use case: Connect Mathematica with another scientific computing system. Suppose that you have already done some work in the other system and have a workspace full of variables. You realize that you want to transfer some of these to Mathematica without having to restart the system.

The key question here is if it is possible to implement a robust connection method which does not care about the order in which the commands are issued. Alternatively, there could be a user-friendly error message (and no bad side effects, such as broken links) when the commands are issued in the wrong order.