Skip to content

Commit

Permalink
Use go 1.21 (#344)
Browse files Browse the repository at this point in the history
* Use go 1.21

This allows us to use context.WithoutCancel and there's no expected incompatible breaking changes.

* Add go.sum from go mod tidy

* remove rand.Seed flagged by golint

* Change threads
  • Loading branch information
sfc-gh-srhodes authored Oct 9, 2023
1 parent ce77029 commit 04285e2
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 56 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/Snowflake-Labs/sansshell

go 1.19
go 1.21

require (
github.com/coreos/go-systemd/v22 v22.5.0
Expand Down
30 changes: 30 additions & 0 deletions go.sum

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions proxy/server/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,3 @@ func (u *unconnectedClientStream) SendMsg(interface{}) error {
func (u *unconnectedClientStream) RecvMsg(interface{}) error {
return fmt.Errorf("%w: RecvMsg", errUnconnectedClient)
}

func init() {
rand.Seed(time.Now().UnixNano())
}
18 changes: 1 addition & 17 deletions services/fdb/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"path/filepath"
"strconv"
"strings"
"time"

"github.com/google/subcommands"
"google.golang.org/protobuf/types/known/durationpb"
Expand Down Expand Up @@ -3709,24 +3708,9 @@ func (r *fdbMoveDataWaitCmd) printCommandOutput(state *util.ExecuteState, idx in
}
}

// This context detachment is temporary until we use go1.21 and context.WithoutCancel is available.
type noCancel struct {
ctx context.Context
}

func (c noCancel) Deadline() (time.Time, bool) { return time.Time{}, false }
func (c noCancel) Done() <-chan struct{} { return nil }
func (c noCancel) Err() error { return nil }
func (c noCancel) Value(key interface{}) interface{} { return c.ctx.Value(key) }

// WithoutCancel returns a context that is never canceled.
func WithoutCancel(ctx context.Context) context.Context {
return noCancel{ctx: ctx}
}

func (r *fdbMoveDataWaitCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
// Ignore the parent context timeout because we don't want to time out here.
ctx = WithoutCancel(ctx)
ctx = context.WithoutCancel(ctx)
state := args[0].(*util.ExecuteState)
c := pb.NewFDBMoveClientProxy(state.Conn)

Expand Down
18 changes: 1 addition & 17 deletions services/httpoverrpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"os"
"strconv"
"strings"
"time"

"github.com/google/subcommands"

Expand Down Expand Up @@ -86,21 +85,6 @@ func (p *proxyCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&p.allowAnyHost, "allow-any-host", false, "Serve data regardless of the Host in HTTP requests instead of only allowing localhost and IPs. False by default to prevent DNS rebinding attacks.")
}

// This context detachment is temporary until we use go1.21 and context.WithoutCancel is available.
type noCancel struct {
ctx context.Context
}

func (c noCancel) Deadline() (time.Time, bool) { return time.Time{}, false }
func (c noCancel) Done() <-chan struct{} { return nil }
func (c noCancel) Err() error { return nil }
func (c noCancel) Value(key interface{}) interface{} { return c.ctx.Value(key) }

// WithoutCancel returns a context that is never canceled.
func WithoutCancel(ctx context.Context) context.Context {
return noCancel{ctx: ctx}
}

func sendError(resp http.ResponseWriter, code int, err error) {
resp.WriteHeader(code)
if _, err := resp.Write([]byte(err.Error())); err != nil {
Expand All @@ -114,7 +98,7 @@ func validatePort(port int) bool {

func (p *proxyCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
// Ignore the parent context timeout because we don't want to time out here.
ctx = WithoutCancel(ctx)
ctx = context.WithoutCancel(ctx)
state := args[0].(*util.ExecuteState)
if f.NArg() != 1 {
fmt.Fprintln(os.Stderr, "Please specify a port to proxy.")
Expand Down
22 changes: 15 additions & 7 deletions services/process/server/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (s *server) GetStacks(ctx context.Context, req *pb.GetStacksRequest) (*pb.G
// New thread so append last entry and start over.
if fields[0] == "Thread" {
// Depending on wrapper/gdb this may have additional fields but we don't need them.
if len(fields) < 6 {
if len(fields) < 4 {
recorder.CounterOrLog(ctx, processGetStacksFailureCounter, 1, attribute.String("reason", "parse_err"))
return nil, status.Errorf(codes.Internal, "unparsable pstack output for new thread: %s", text)
}
Expand All @@ -244,13 +244,21 @@ func (s *server) GetStacks(ctx context.Context, req *pb.GetStacksRequest) (*pb.G
recorder.CounterOrLog(ctx, processGetStacksFailureCounter, 1, attribute.String("reason", "parse_err"))
return nil, status.Errorf(codes.Internal, "can't parse thread number: %s : %v", text, err)
}
if n, err := fmt.Sscanf(fields[3], "0x%x", &stack.ThreadId); n != 1 || err != nil {
recorder.CounterOrLog(ctx, processGetStacksFailureCounter, 1, attribute.String("reason", "parse_err"))
return nil, status.Errorf(codes.Internal, "can't parse thread id: %s : %v", text, err)
if fields[2] == "(Thread" && len(fields) >= 6 {
if n, err := fmt.Sscanf(fields[3], "0x%x", &stack.ThreadId); n != 1 || err != nil {
recorder.CounterOrLog(ctx, processGetStacksFailureCounter, 1, attribute.String("reason", "parse_err"))
return nil, status.Errorf(codes.Internal, "can't parse thread id: %s : %v", text, err)
}
if n, err := fmt.Sscanf(fields[5], "%d", &stack.Lwp); n != 1 || err != nil {
recorder.CounterOrLog(ctx, processGetStacksFailureCounter, 1, attribute.String("reason", "parse_err"))
return nil, status.Errorf(codes.Internal, "can't parse lwp: %s : %v", text, err)
}
}
if n, err := fmt.Sscanf(fields[5], "%d", &stack.Lwp); n != 1 || err != nil {
recorder.CounterOrLog(ctx, processGetStacksFailureCounter, 1, attribute.String("reason", "parse_err"))
return nil, status.Errorf(codes.Internal, "can't parse lwp: %s : %v", text, err)
if fields[2] == "(LWP" {
if n, err := fmt.Sscanf(fields[3], "%d", &stack.Lwp); n != 1 || err != nil {
recorder.CounterOrLog(ctx, processGetStacksFailureCounter, 1, attribute.String("reason", "parse_err"))
return nil, status.Errorf(codes.Internal, "can't parse lwp: %s : %v", text, err)
}
}
numEntries++
continue
Expand Down
20 changes: 12 additions & 8 deletions services/process/server/process_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ var (
"./testdata/linux_bad3.ps", // bad nice value
}

testdataPstackNoThreads = "./testdata/linux_pstack_no_threads.txt"
testdataPstackNoThreadsTextProto = "./testdata/linux_pstack_no_threads.textproto"
testdataPstackThreads = "./testdata/linux_pstack_threads.txt"
testdataPstackThreadsTextProto = "./testdata/linux_pstack_threads.textproto"
testdataPstackThreadsBadThread = "./testdata/linux_pstack_threads_bad_thread.txt"
testdataPstackThreadsBadThreadNumber = "./testdata/linux_pstack_threads_bad_thread_number.txt"
testdataPstackThreadsBadThreadID = "./testdata/linux_pstack_threads_bad_thread_id.txt"
testdataPstackThreadsBadLwp = "./testdata/linux_pstack_threads_bad_lwp.txt"
testdataPstackNoThreads = "./testdata/linux_pstack_no_threads.txt"
testdataPstackNoThreadsTextProto = "./testdata/linux_pstack_no_threads.textproto"
testdataPstackThreads = "./testdata/linux_pstack_threads.txt"
testdataPstackThreadsTextProto = "./testdata/linux_pstack_threads.textproto"
testdataPstackSingleThread = "./testdata/linux_pstack_single_thread.txt"
testdataPstackSingleThreadTextProto = "./testdata/linux_pstack_single_thread.textproto"
testdataPstackSingleThreadWithLWP = "./testdata/linux_pstack_single_thread_with_lwp.txt"
testdataPstackSingleThreadWithLWPTextProto = "./testdata/linux_pstack_single_thread_with_lwp.textproto"
testdataPstackThreadsBadThread = "./testdata/linux_pstack_threads_bad_thread.txt"
testdataPstackThreadsBadThreadNumber = "./testdata/linux_pstack_threads_bad_thread_number.txt"
testdataPstackThreadsBadThreadID = "./testdata/linux_pstack_threads_bad_thread_id.txt"
testdataPstackThreadsBadLwp = "./testdata/linux_pstack_threads_bad_lwp.txt"
)
18 changes: 16 additions & 2 deletions services/process/server/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ func TestPstackNative(t *testing.T) {
})
testutil.FatalOnErr("can't get native pstack", err, t)

// We're a go program. We have multiple threads.
if len(resp.Stacks) <= 1 {
// We're a go program. We have one or more threads.
if len(resp.Stacks) < 1 {
t.Fatalf("Not enough threads in native response. Response: %+v", prototext.Format(resp))
}

Expand Down Expand Up @@ -372,6 +372,20 @@ func TestPstack(t *testing.T) {
validate: testdataPstackThreadsTextProto,
pid: 1,
},
{
name: "A program with a single thread",
command: testutil.ResolvePath(t, "cat"),
input: testdataPstackSingleThread,
validate: testdataPstackSingleThreadTextProto,
pid: 1,
},
{
name: "A program with a single thread with an lwp",
command: testutil.ResolvePath(t, "cat"),
input: testdataPstackSingleThreadWithLWP,
validate: testdataPstackSingleThreadWithLWPTextProto,
pid: 1,
},
{
name: "bad pid - zero",
command: testutil.ResolvePath(t, "cat"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
stacks : <
thread_number : 1
stacks : "#0 0x00000000000807ec in runtime.futex.abi0 ()"
stacks : "#1 0x000000000004556c in runtime.futexsleep ()"
>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Thread 1 (process 2947021):
#0 0x00000000000807ec in runtime.futex.abi0 ()
#1 0x000000000004556c in runtime.futexsleep ()
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
stacks : <
thread_number : 9
lwp : 19049
stacks : "#0 0x00000000000807ec in runtime.futex.abi0 ()"
stacks : "#1 0x000000000004556c in runtime.futexsleep ()"
>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Thread 9 (LWP 19049 "go"):
#0 0x00000000000807ec in runtime.futex.abi0 ()
#1 0x000000000004556c in runtime.futexsleep ()

0 comments on commit 04285e2

Please sign in to comment.