Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle plugin execute failure case #185

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions managedplugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,20 @@ func (c *Client) startLocal(ctx context.Context, path string) error {
c.wg.Add(1)
go c.readLogLines(reader)

return c.connectToUnixSocket(ctx, cmd)
err = c.connectToUnixSocket(ctx)
if err == nil {
return err
}

if killErr := cmd.Process.Kill(); killErr != nil {
c.logger.Error().Err(killErr).Msg("failed to kill plugin process")
}

waitErr := cmd.Wait()
if waitErr != nil && errors.Is(err, context.DeadlineExceeded) {
return fmt.Errorf("failed to run plugin %s: %w", path, waitErr)
}
return fmt.Errorf("failed connecting to plugin %s: %w", path, err)
}

func (c *Client) getPluginArgs() []string {
Expand Down Expand Up @@ -455,29 +468,26 @@ func (c *Client) connectUsingTCP(ctx context.Context, path string) error {
)
}

func (c *Client) connectToUnixSocket(ctx context.Context, cmd *exec.Cmd) error {
func (c *Client) connectToUnixSocket(ctx context.Context) error {
dialer := func(ctx context.Context, addr string) (net.Conn, error) {
d := &net.Dialer{
Timeout: 5 * time.Second,
}
return d.DialContext(ctx, "unix", addr)
}
ktx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

var err error
c.Conn, err = grpc.DialContext(ctx, c.grpcSocketName,
c.Conn, err = grpc.DialContext(ktx, c.grpcSocketName,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from the godoc:

In the blocking case, ctx can be used to cancel or expire the pending connection. Once this function returns, the cancellation and expiration of ctx will be noop.

grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
grpc.WithContextDialer(dialer),
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(maxMsgSize),
grpc.MaxCallSendMsgSize(maxMsgSize),
))
if err != nil {
if err := cmd.Process.Kill(); err != nil {
c.logger.Error().Err(err).Msg("failed to kill plugin process")
}
return err
}
return nil
return err
}

func (c *Client) Name() string {
Expand Down
Loading