AMS Sdk Potential Deadlock in `createApplication` example Due to Channel Send/Receive Order (fixed)

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:

  1. Compile and execute createApplication example with the current implementation.
  2. Observe that the function may block if c.CreateApplication sends messages to sentBytesCh 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

Hi @kernelmode ,

Thanks for reporting this and also attaching the fix. We will include this soon in a fix.

Hi @kernelmode,

This has now been fixed in 1.20 release of anbox cloud.