Hello,
I’ve encountered a potential issue in the createApplication
ams sdk example which can lead to a deadlock situation due to the order of operations related to channel communication.
Description:
In the current implementation of the createApplication
example, the c.CreateApplication(packagePath, sentBytesCh)
method is called before starting the goroutine that consumes messages from the sentBytesCh
channel. If c.CreateApplication
starts sending messages to the sentBytesCh
channel immediately, and there’s no consumer ready to receive those messages, the function can block indefinitely. This is because sending to an unbuffered channel in Go will block until there’s a receiver ready to receive the message.
Steps to Reproduce:
- Compile and execute
createApplication
example with the current implementation. - Observe that the function may block if
c.CreateApplication
sends messages tosentBytesCh
before the goroutine is started.
Suggested Fix:
To avoid this potential deadlock, the goroutine that consumes messages from the sentBytesCh
channel should be started before calling the c.CreateApplication
method. This ensures that there’s a consumer ready to receive messages as soon as they’re sent to the channel.
// Start the goroutine first
go func() {
for {
select {
case n := <-sentBytesCh:
log.Printf("Sent: %v", n)
case <-closeCh:
return
}
}
}()
// Then call the method
operation, err := c.CreateApplication(packagePath, sentBytesCh)
Best,
Paolo