From 42b09547c7223703a24fd4b19595d56a799b2805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jerome=20K=C3=BCttner?= Date: Wed, 18 Jun 2025 18:16:43 +0200 Subject: [PATCH 1/3] close io.copy functions --- pkg/proc/job_listener.go | 25 +++++++++++++++++++------ pkg/proc/runner.go | 2 ++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/pkg/proc/job_listener.go b/pkg/proc/job_listener.go index d15f2e7..0c1ef9c 100644 --- a/pkg/proc/job_listener.go +++ b/pkg/proc/job_listener.go @@ -141,23 +141,36 @@ func (l *Listener) run(ctx context.Context) <-chan error { fromUpstreamErrors := make(chan error) go func() { + defer func() { + // this sends an EOF to the other io.Copy-Command + if tcpUpstream, ok := upstream.(*net.TCPConn); ok { + tcpUpstream.CloseWrite() + } + close(toUpstreamErrors) + }() + if _, err := io.Copy(upstream, conn); err != nil && !errors.Is(err, io.EOF) { toUpstreamErrors <- err } - close(toUpstreamErrors) }() go func() { + // this sends an EOF to the other io.Copy-Command + defer func() { + if tcpConn, ok := conn.(*net.TCPConn); ok { + tcpConn.CloseWrite() + } + close(fromUpstreamErrors) + }() + if _, err := io.Copy(conn, upstream); err != nil && !errors.Is(err, io.EOF) { fromUpstreamErrors <- err } - close(fromUpstreamErrors) }() - select { - case <-toUpstreamErrors: - case <-fromUpstreamErrors: - } + // wait for both channels (do not use `select-case`) + <-toUpstreamErrors + <-fromUpstreamErrors }() } }() diff --git a/pkg/proc/runner.go b/pkg/proc/runner.go index 62d33c0..4cdc23b 100644 --- a/pkg/proc/runner.go +++ b/pkg/proc/runner.go @@ -3,6 +3,7 @@ package proc import ( "context" "fmt" + "runtime" "sync" "time" @@ -104,6 +105,7 @@ func (r *Runner) Run() error { // watch files case <-ticker.C: + log.Debugf("active goroutines: %d", runtime.NumGoroutine()) for _, job := range r.jobs { job.Watch() if r.keepRunning { From c849384a62e6ea7eae48b57aaa6f62fa605ae8a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jerome=20K=C3=BCttner?= Date: Fri, 20 Jun 2025 09:50:14 +0200 Subject: [PATCH 2/3] add env for debug logging --- main.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/main.go b/main.go index ecbe5b5..d120377 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "github.com/mittwald/mittnite/cmd" log "github.com/sirupsen/logrus" + "os" ) func init() { @@ -10,6 +11,9 @@ func init() { Formatter.TimestampFormat = "02-01-2006 15:04:05" Formatter.FullTimestamp = true log.SetFormatter(Formatter) + if os.Getenv("MITTNITE_LOG_LEVEL") == "debug" { + log.SetLevel(log.DebugLevel) + } } func main() { From d634844509edcaa76fac2bea2dead1bc9750f07a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jerome=20K=C3=BCttner?= Date: Fri, 20 Jun 2025 09:52:01 +0200 Subject: [PATCH 3/3] remove obsolete comments --- pkg/proc/job_listener.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/proc/job_listener.go b/pkg/proc/job_listener.go index 0c1ef9c..f6e034f 100644 --- a/pkg/proc/job_listener.go +++ b/pkg/proc/job_listener.go @@ -142,7 +142,6 @@ func (l *Listener) run(ctx context.Context) <-chan error { go func() { defer func() { - // this sends an EOF to the other io.Copy-Command if tcpUpstream, ok := upstream.(*net.TCPConn); ok { tcpUpstream.CloseWrite() } @@ -155,7 +154,6 @@ func (l *Listener) run(ctx context.Context) <-chan error { }() go func() { - // this sends an EOF to the other io.Copy-Command defer func() { if tcpConn, ok := conn.(*net.TCPConn); ok { tcpConn.CloseWrite()