Skip to content

Commit

Permalink
feat(java): process dead handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jclab-joseph committed Mar 24, 2023
1 parent 2f1e825 commit 6ffbe40
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package kr.jclab.sipc.exception;

public class SipcProcessDeadException extends SipcHandshakeException {
public SipcProcessDeadException() {
}

public SipcProcessDeadException(String message) {
super(message);
}

public SipcProcessDeadException(String message, Throwable cause) {
super(message, cause);
}

public SipcProcessDeadException(Throwable cause) {
super(cause);
}

public SipcProcessDeadException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
15 changes: 15 additions & 0 deletions java/core/src/main/java/kr/jclab/sipc/internal/Process9Helper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package kr.jclab.sipc.internal;

import javax.annotation.Nullable;
import java.lang.reflect.Method;
import java.util.concurrent.CompletableFuture;

public class Process9Helper {
public static @Nullable CompletableFuture<Process> onExitIfAvailable(Process process) {
try {
Method method = Process.class.getMethod("onExit");
return (CompletableFuture<Process>) method.invoke(process);
} catch (Exception e) {}
return null;
}
}
63 changes: 56 additions & 7 deletions java/core/src/main/java/kr/jclab/sipc/server/SipcChild.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import io.netty.channel.ChannelHandler;
import kr.jclab.sipc.exception.SipcHandshakeTimeoutException;
import kr.jclab.sipc.exception.NotYetConnectedException;
import kr.jclab.sipc.exception.SipcProcessDeadException;
import kr.jclab.sipc.internal.DeferredInt;
import kr.jclab.sipc.internal.PidAccessor;
import kr.jclab.sipc.internal.Process9Helper;
import kr.jclab.sipc.proto.SipcProto;
import kr.jclab.sipc.server.internal.SipcChildChannelContext;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import javax.annotation.Nullable;
import java.util.Base64;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -49,6 +52,18 @@ public void attachProcess(Process process) {
this.process = process;
this.pid.set((int) PidAccessor.getPid(process, this.parent.serverContext.getWindowsNativeSupport()));
start();

CompletableFuture<Process> onExit = Process9Helper.onExitIfAvailable(process);
System.out.println("onExit : " + onExit);
if (onExit != null) {
onExit.whenComplete((proc, ex) -> {
if (ex != null) {
onProcessDead(ex);
} else {
onProcessDead();
}
});
}
}

public void attachProcess(int pid) {
Expand Down Expand Up @@ -76,19 +91,53 @@ private synchronized void start() {
}
}

private synchronized void onHandshakeTimeout() {
if (this.childChannelContext != null) {
return;
public synchronized void onProcessDead() {
if (this.childChannelContext == null) {
handshakeFailure(new SipcProcessDeadException());
} else {
remove(null);
}
}

parent.serverContext.removeChild(connectInfo.getConnectionId());
public synchronized void onProcessDead(Throwable e) {
if (this.childChannelContext == null) {
handshakeFailure(new SipcProcessDeadException(e));
} else {
remove(e);
}
}

if (!handshakeFuture.isDone()) {
handshakeFuture.completeExceptionally(new SipcHandshakeTimeoutException());
private synchronized void onHandshakeTimeout() {
handshakeFailure(new SipcHandshakeTimeoutException());
}

private synchronized void handshakeFailure(Throwable cause) {
try {
if (this.childChannelContext != null) {
return;
}

if (handshakeTimeoutFuture != null) {
handshakeTimeoutFuture.cancel(false);
}

if (!handshakeFuture.isDone()) {
handshakeFuture.completeExceptionally(cause);
}

if (this.channelHandler instanceof SipcServerChannelHandler) {
((SipcServerChannelHandler) this.channelHandler).onHandshakeFailed(this, cause);
}
} finally {
remove(cause);
}
}

private synchronized void remove(@Nullable Throwable cause) {
parent.serverContext.removeChild(connectInfo.getConnectionId());

if (this.channelHandler instanceof SipcServerChannelHandler) {
((SipcServerChannelHandler) this.channelHandler).onHandshakeTimeout(this);
((SipcServerChannelHandler) this.channelHandler).onRemoved(this, cause);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import io.netty.channel.ChannelHandler;

import javax.annotation.Nullable;

public interface SipcServerChannelHandler extends ChannelHandler {
void onHandshakeTimeout(SipcChild child);
void onHandshakeFailed(SipcChild child, Throwable cause);

void onRemoved(SipcChild child, @Nullable Throwable cause);
}

0 comments on commit 6ffbe40

Please sign in to comment.