<div dir="ltr">I came across this in the LXD test suite today, which was hard to track down, so I figured I'd let everyone know about it.<div><br></div><div>We have a nice helper in testing.IsolationSuite with "PatchValue()" that will change a global for you during the test, and then during TearDown() will cleanup the patch it made.</div><div>It turns out that if your test doesn't have a pointer receiver this fails, because the "suite" object is a copy, so when PatchValue calls AddCleanup to do s.testStack = append(...) the suite object goes away before TearDown is called.</div><div><br></div><div>You can see this with the attached test suite.</div><div><br></div><div>Example:</div><div><br></div><div>func (s mySuite) TestFoo(c *gc.C) {</div><div> // This is unsafe because s.PatchValue ends up modifying s.testStack but that attribute only exists </div><div> // for the life of the TestFoo function</div><div>  s.PatchValue(&globalVar, "newvalue")</div><div>}</div><div><br></div><div>I tried adding the attached patch so that we catch places we are using AddCleanup unsafely, but it fails a few tests I wasn't expecting, so I'm not sure if I'm actually doing the right thing.</div><div><br></div><div>John</div><div>=:-></div><div><br></div></div>