Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: clear() on map doesn't correctly clear existing iterators #59411

Closed
randall77 opened this issue Apr 4, 2023 · 2 comments
Closed

runtime: clear() on map doesn't correctly clear existing iterators #59411

randall77 opened this issue Apr 4, 2023 · 2 comments
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker
Milestone

Comments

@randall77
Copy link
Contributor

This program shouldn't print anything:

package main

import "math"

func main() {
	for i := 0; i < 100; i++ {
		f()
	}
}
func f() {
	// Allocate map.
	m := map[float64]int{}
	// Fill to just before a growth trigger.
	const N = 13 << 4 // 6.5 * 2 * 2^k
	for i := 0; i < N; i++ {
		m[math.NaN()] = i
	}
	// Trigger growth.
	m[math.NaN()] = N

	// Iterate through map.
	i := 0
	for _, v := range m {
		if i == 6 {
			// Partway through iteration, clear the map.
			clear(m)
		} else if i > 6 {
			// If we advance to the next iteration, that's a bug.
			println("BAD", i, v)
		}
		i++
	}
}

It does (at tip).

For maps whose keys have NaNs in them, iterators that are in progress can't tell that some entries have been deleted by the clear builtin. This is because where those NaNs should move to in the larger bucket array isn't computable. Pre clear bultin, this didn't matter because the NaN entries couldn't be deleted.

@cuonglm

Update #55002, #56351

@randall77 randall77 added this to the Go1.21 milestone Apr 4, 2023
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Apr 4, 2023
@cuonglm
Copy link
Member

cuonglm commented Apr 4, 2023

@randall77 Could we mark all bucket slot as emptyOne?

@gopherbot
Copy link

Change https://go.dev/cl/481935 mentions this issue: runtime: mark map bucket slots as empty during map clear

@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 4, 2023
@golang golang locked and limited conversation to collaborators Apr 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker
Projects
None yet
Development

No branches or pull requests

4 participants