Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Duplicate name when creating a new workspace build (transition): pq: duplicate key value violates unique constraint "workspace_builds_workspace_id_name_key" #3038

Closed as not planned
@mafredri

Description

@mafredri

This is an issue we ran into CI, it's a rare occurrence but there's a non-zero chance it can happen to our users as well.

Problem: When a new workspace build is created for an existing workspace (e.g. transition into deleted) a new (unique-ish) name is generated for that job via namesgenerator.GetRandomName(1). Normally this should not be an issue, but we have a unique constraint on (workspace_id, name):

ALTER TABLE ONLY workspace_builds
    ADD CONSTRAINT workspace_builds_workspace_id_name_key UNIQUE (workspace_id, name);

This results in a non-zero chance that both the create job and delete job receive the same name, resulting in the conflict. For very long lived workspaces, the chance of hitting this issue becomes greater. I did a local test that reproduced this after running workspace start/stop 200 times.

The CI run that hit this issue: https://github.com/coder/coder/runs/7399324482?check_suite_focus=true

2022-07-18T22:52:32.7769558Z     workspaces_test.go:331: 
2022-07-18T22:52:32.7770135Z         	Error Trace:	/home/runner/work/coder/coder/coderd/workspaces_test.go:331
2022-07-18T22:52:32.7770615Z         	Error:      	Received unexpected error:
2022-07-18T22:52:32.7772201Z         	            	POST http://127.0.0.1:42617/api/v2/workspaces/9a172a87-b686-4a93-a286-70d473562375/builds: unexpected status code 500: Internal error inserting workspace build.
2022-07-18T22:52:32.7773646Z         	            		Error: execute transaction: insert workspace build: pq: duplicate key value violates unique constraint "workspace_builds_workspace_id_name_key"
2022-07-18T22:52:32.7774246Z         	Test:       	TestWorkspaceByOwnerAndName/Deleted
2022-07-18T22:52:32.7774730Z         	Messages:   	delete the workspace
2022-07-18T22:52:32.7775638Z     t.go:81: 2022-07-18 22:49:15.883 [DEBUG]	(provisionerd)	<github.com/coder/coder/provisionerd/provisionerd.go:370>	(*Server).closeWithError	closing server with error	{"error": null}
2022-07-18T22:52:32.7776288Z     --- FAIL: TestWorkspaceByOwnerAndName/Deleted (6.92s)
2022-07-18T22:52:32.7776505Z 
2022-07-18T22:52:32.7776777Z make: *** [Makefile:175: test-postgres] Error 1

Questions

  • Is there a good reason to keep unique names for all workspace builds (and all transitions)?
  • Should we try a new name on conflict?

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiArea: HTTP API

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions