tracker issue : CF-4207397

select a category, or use search below
(searches all categories and all time range)

Race Condition (for in loop + single statement + struct notation)

| View in Tracker

Status/Resolution/Reason: To Fix//BugVerified

Reporter/Name(from Bugbase): Chris P. / ()

Created: 03/03/2020

Components: Language, CFSCRIPT

Versions: 2016,2018

Failure Type: Data Corruption

Found In Build/Fixed In Build: 2016.0.13.316217 /

Priority/Frequency: Normal / Most users will encounter

Locale/System: English / Win 2016

Vote Count: 2

# Problem Description:

Referencing the item in a (for in) loop when building a struct key and the struct value as a single statement, has a race condition when run concurrently.

# Steps to Reproduce:

(See attached zip. It has a self-contained reproducible case.)

Something like:
	public struct function getItemMap() {
		var items = getItems(); // returns array of 5 items
		var itemMap = {};
		for ( var item in items ) {
			// using struct notation _while_ assigning will cause the bug.
			itemMap["item_" &] = {
				"...": "..." // more fields
		return itemMap; // often returns arrays of wrong length and keys don't match items

# Actual Result:

Very frequently, you will get:
* A struct that doesn't contain a key for each item (some missing)
* A struct where the values don't line up with their keys

# Expected Result:

* A struct that contains as many keys as the "items" array contained.
* The key (e.g. "item_3" to match the key value's "key" property (e.g. 3)

# Any Workarounds:

* Create the struct first (assigning to a throw away variable)
* Then, assign the throw away variable to the struct at the key

# Notes
* You should be able to see the problem easily if you unzip my example and run it from anywhere in a CF webroot.
* This would be a common code pattern for producing a "map" with items in the "map" addressable by their "id" or some convention based key.