~rockorager/go-jmap-devel

client: lock mutex before assigning Session v1 PROPOSED

Silvan Jegen: 2
 client: lock mutex before assigning Session
 client: don't double unlock if no session exists already

 2 files changed, 6 insertions(+), 1 deletions(-)
"Tim Culverhouse" <tim@timculverhouse.com> wrote:
Next
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~rockorager/go-jmap-devel/patches/56752/mbox | git am -3
Learn more about email & git

[PATCH 1/2] client: lock mutex before assigning Session Export this patch

If we don't do this, there might be concurrent access if multiple "Do()"s
are executed at the same time without having called "Authenticate()"
before.
---
 client.go | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/client.go b/client.go
index 6246874..3b541a9 100644
--- a/client.go
+++ b/client.go
@@ -95,7 +95,11 @@ func (c *Client) Authenticate() error {
	if err != nil {
		return err
	}

	c.Lock()
	c.Session = s
	c.Unlock()

	return nil
}

-- 
2.47.1

[PATCH 2/2] client: don't double unlock if no session exists already Export this patch

We should only call Unlock in the case that we didn't call Authenticate()
before, otherwise we would unlock the mutex twice (which will cause a
runtime error).
---
 client.go | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/client.go b/client.go
index 3b541a9..33aa553 100644
--- a/client.go
+++ b/client.go
@@ -112,8 +112,9 @@ func (c *Client) Do(req *Request) (*Response, error) {
		if err != nil {
			return nil, err
		}
	} else {
		c.Unlock()
	}
	c.Unlock()
	// Check the required capabilities before making the request
	for _, uri := range req.Using {
		c.Lock()
-- 
2.47.1