Skip to content

x/website/_content/doc/tutorial: "Developing a RESTful API with Go and Gin" has unmentioned race condition #51097

Open
@vearutop

Description

@vearutop

This tutorial https://go.dev/doc/tutorial/web-service-gin uses package variable album to read and write from http handlers.
However there is no synchronization applied to avoid race condition.

// postAlbums adds an album from JSON received in the request body.
func postAlbums(c *gin.Context) {
    var newAlbum album

    // Call BindJSON to bind the received JSON to
    // newAlbum.
    if err := c.BindJSON(&newAlbum); err != nil {
        return
    }

    // Add the new album to the slice.
    albums = append(albums, newAlbum)                       // <-- DATA RACE.
    c.IndentedJSON(http.StatusCreated, newAlbum)
}

If this is intentional to keep the tutorial simple I think it makes sense to warn the reader about code limitations, because high level of concurrency is natural for http servers.

Or maybe it would be even better to avoid race with a different design.

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.website

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions