Subtle, unexpected behaviour of Golang's slice "append"

Python leaky unittests Gotcha - Too Many MySQL Connections

OpenWorm - a long way to go

Open Source Software Presentation

I am a software engineer (lapsed physicist) who likes working with open source technology. I spend most of my time writing code in Python but I am currently also learning Go.

In Go, a slice describes a segment of an array. It consists of

  1. a pointer to the array
  2. the length of the segment
  3. its capacity (the maximum length of the segment).

go slice

When calling append on a slice, if the length of the slice after the append operation is larger than the underlying array (the capacity, in Go-speak), a new array is instantiated to hold the new data (The new array is is much larger, to accommodate more data being added). As a consequence, any other slice previously referring to the same underlying array now refers to a different array. This gives rise to some slightly strange behaviour, best shown in this example:

package main

import (

func main() {
    a := []int{1,2,3}
    slice1 := a[:2]
    slice2 := a[:2]
    slice2 = append(slice2,4) // first append
    slice2[0] = 8 // underlying array still the same, both slice 1 and slice 2 have element 0 == 8
    fmt.Println("slice 1:", slice1) // slice 1: [8 2]
    fmt.Println("slice 2:", slice2) // slice 2: [8 2 4]
    slice2 = append(slice2,4) // array capacity now exceeded, new underlying array created
    slice2[0] = 3 // slice1 still has element 0 == 8, but slice 2 has element 0 == 3
    fmt.Println("slice 1:", slice1) // slice 1: [8 2]
    fmt.Println("slice 2:", slice2) // slice 2: [3 2 4 4]

At the begining, slice1 and slice2 above referred to the underlying array. When an append is performed on slice2 the underlying array still has sufficient capacity and so an in-place change to slice2 shows up in slice1. However, when the second append happens the underlying array is not big enough so slice2 refers to a new array with a larger capacity. Therefore, any changes to slice2 are "out of sync" with slice1.

This kind of thing is something you're unlikely to encounter too often, but is good to be aware of. In general, it seems like having slices refer to the same underlying array is probably best avoided, unless it's for a very specific reason.

Some time ago I noticed that some of my integration tests were failing with a MySQL Too many connections error. The tests were being run with Python's unittest package. Each integration test was opening a MySQL connection, but I couldn't understand the cause of this error - each test was run with a new TestInstance class and I (wrongly) assumed Python's garbage collector would come along and clear the old TestInstance, closing the MySQL connection in the process.

Heading over to Stack Overflow eventually gave me the answer (thanks Kara!)

It turns out that a reference to each test case is kept until the entire test suite is run, preventing the garbage collector from freeing this object

So the following TestCase will have opened 3 simultaneous MySQL connections by the time it terminates:

import unittest
import MySQLdb

class TestMySQLConn(unittest.TestCase):
    def setUp(self):
        self.db_connection = MySQLdb.connect(some_credentials)

    def test1(self):
        # a MySQL connection opens here

    def test2(self):
        # another MySQL connection opens here

    def test3(self):
        # yet another MySQL connection opens here

The best solution is to use a tearDown method in the TestCase:

def tearDown(self):

This problem was eventually discussed and resolved in Python3.4

OpenWorm is a fantastic project which it has been my pleasure to work on for two years. In that time I have seen the community go from strength to strength and our prospects of making meaningful scientific contributions continue to grow.

It's recently been driven home to me how cautious one has to be when reading articles in the media about science. It all started with a video I released to our mailing list. This was intended as a very early proof of concept - showing that our method of simulating a worm muscular system as blocks of PCISPH contractile matter had the potential to reproduce undulutory thrust - an exciting discovery, but one filled with subtle caveats.

A few days later this BBC news article about OpenWorm surfaced. It's fantastic to see how much interest there is in our project, but the article was (while generally OK) inaccurate in a few ways. John Hurliman for instance is incorrectly attributed as project leader (OpenWorm has no "leader") and the project has been going on for much longer than suggested, the scientific claims were broadly correct though. Over time more articles were released, with the claims made becoming a bit outlandish, culminating in this. There were also some really good articles - the one on being my personal favourite.

We have released a blog post addressing our concerns about reporting.


I recently gave a workshop on Open Source Software development at the Malta Information Technology Agency.

It was a great pleasure and I would like to thank all who came for participating. The slides are located here.