From 901929356fb7c09e69e23ef3f3a7a178ee03b9d3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 20 Dec 2013 22:01:38 -0800 Subject: [PATCH] packer/rpc: don't use stream ID zero [GH-738] --- packer/rpc/muxconn.go | 6 ++++++ packer/rpc/muxconn_test.go | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packer/rpc/muxconn.go b/packer/rpc/muxconn.go index 5c865b944..d3200d843 100644 --- a/packer/rpc/muxconn.go +++ b/packer/rpc/muxconn.go @@ -189,6 +189,12 @@ func (m *MuxConn) NextId() uint32 { m.muAccept.Lock() defer m.muAccept.Unlock() + // We never use stream ID 0 because 0 is the zero value of a uint32 + // and we want to reserve that for "not in use" + if m.curId == 0 { + m.curId = 1 + } + for { result := m.curId m.curId += 1 diff --git a/packer/rpc/muxconn_test.go b/packer/rpc/muxconn_test.go index a2f76bf3b..9e67454a4 100644 --- a/packer/rpc/muxconn_test.go +++ b/packer/rpc/muxconn_test.go @@ -241,14 +241,14 @@ func TestMuxConnNextId(t *testing.T) { a := client.NextId() b := client.NextId() - if a != 0 || b != 1 { + if a != 1 || b != 2 { t.Fatalf("IDs should increment") } a = server.NextId() b = server.NextId() - if a != 0 || b != 1 { + if a != 1 || b != 2 { t.Fatalf("IDs should increment: %d %d", a, b) } }