@@ -15,7 +15,9 @@ import (
1515 "github.com/ydb-platform/ydb-go-sdk/v3/coordination"
1616 "github.com/ydb-platform/ydb-go-sdk/v3/coordination/options"
1717 "github.com/ydb-platform/ydb-go-sdk/v3/internal/coordination/conversation"
18+ "github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
1819 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
20+ "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1921 "github.com/ydb-platform/ydb-go-sdk/v3/trace"
2022)
2123
@@ -59,13 +61,13 @@ func createSession(
5961 client .sessionCreated (& s )
6062
6163 sessionStartedChan := make (chan struct {})
62- go s .mainLoop (path , sessionStartedChan )
64+ go s .mainLoop (xcontext . ValueOnly ( ctx ), path , sessionStartedChan )
6365
6466 select {
6567 case <- ctx .Done ():
6668 cancel ()
6769
68- return nil , ctx .Err ()
70+ return nil , xerrors . WithStackTrace ( ctx .Err () )
6971 case <- sessionStartedChan :
7072 }
7173
@@ -108,6 +110,30 @@ func (s *session) updateCancelStream(cancel context.CancelFunc) {
108110 s .cancelStream = cancel
109111}
110112
113+ func newSessionClient (
114+ ctx context.Context , client Ydb_Coordination_V1.CoordinationServiceClient , t * trace.Coordination ,
115+ ) (
116+ _ Ydb_Coordination_V1.CoordinationService_SessionClient , cancel context.CancelFunc , finalErr error ,
117+ ) {
118+ streamCtx , streamCancel := xcontext .WithCancel (xcontext .ValueOnly (ctx ))
119+
120+ onDone := trace .CoordinationOnNewSessionClient (t , & ctx ,
121+ stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/coordination.newSessionClient" ),
122+ )
123+ defer func () {
124+ onDone (finalErr )
125+ }()
126+
127+ sessionClient , err := client .Session (streamCtx )
128+ if err != nil {
129+ streamCancel ()
130+
131+ return nil , nil , xerrors .WithStackTrace (err )
132+ }
133+
134+ return sessionClient , streamCancel , nil
135+ }
136+
111137// Create a new gRPC stream using an independent context.
112138//
113139//nolint:funlen
@@ -129,14 +155,14 @@ func (s *session) newStream(
129155 for {
130156 result := make (chan Ydb_Coordination_V1.CoordinationService_SessionClient , 1 )
131157 go func () {
132- var err error
133- onDone := trace . CoordinationOnStreamNew ( s . client . config . Trace ())
134- defer func () {
135- onDone ( err )
136- }()
137-
138- client , err := s . client . client . Session ( streamCtx )
139- result <- client
158+ client , cancel , err := newSessionClient ( streamCtx , s . client . client , s . client . config . Trace ())
159+ if err == nil {
160+ select {
161+ case result <- client :
162+ default :
163+ cancel ()
164+ }
165+ }
140166 }()
141167
142168 var client Ydb_Coordination_V1.CoordinationService_SessionClient
@@ -220,7 +246,7 @@ func (s *session) mainLoop(path string, sessionStartedChan chan struct{}) {
220246 // We intentionally place a stream context outside the scope of any existing contexts to make an attempt to
221247 // close the session gracefully at the end of the main loop.
222248
223- streamCtx , cancelStream := context .WithCancel (context . Background () )
249+ streamCtx , cancelStream := context .WithCancel (ctx )
224250 sessionClient , err := s .newStream (streamCtx , cancelStream )
225251 if err != nil {
226252 // Giving up, we can do nothing without a stream.
0 commit comments