Skip to content

Accessing non-intialised state object in JsCmd race condition #239

@johnaohara

Description

@johnaohara

There appears to be a race condition in accessing a state object in a JsCmd that has not been initialized.

Take for example the following qDup script:

scripts:
  pushState:
  - set-state: RUN.MEASUREMENTS []
  - set-state: RUN.MEASUREMENTS_OBJ []
  - set-state: RUN.MEASUREMENTS_OBJ_NON_INITIALIZED []
  
  - for-each: ITERATION ${{=[...Array(${{ITERATIONS}}).keys()]}}
    then:
    - sleep: 1s
    #scalar value
    - set-state: measurement ${{ITERATION}}
    - js: |
        (input,state) => {
            state.MEASUREMENTS.push( state.measurement )
        }

    #intialized object
    - set-state: measure {}
    - sh: date +%s
      then:
      - set-state: measure.timestamp
    - set-state: measure.JOULES_CUR ${{ITERATION}}
    - log: ${{measure}}
    - js: |
        (input,state) => {
            state.MEASUREMENTS_OBJ.push( state.measure )
        }
        
    #non-intialized object
    - sh: date +%s
      then:
      - set-state: measure_non_intialized.timestamp
    - set-state: measure_non_intialized.JOULES_CUR ${{ITERATION}}
    - log: ${{measure}}
    - js: |
        (input,state) => {
            state.MEASUREMENTS_OBJ_NON_INITIALIZED.push( state.measure_non_intialized )
        }

hosts:
  target: LOCAL

roles:
  run:
    hosts: [target]
    run-scripts:
    - pushState

states:
  ITERATIONS: 5

I would expect the three global arrays to be populated with an arrays containing increasing arithmetic sequences.

However, in the "non-intialized" case, the RUN.MEASUREMENTS_OBJ_NON_INITIALIZED array contains a set of objects that are all equal to the last object created by the script, e.g.

{
  "MEASUREMENTS": [
    0,
    1,
    2,
    3,
    4
  ],
  "ITERATIONS": "5",
  "MEASUREMENTS_OBJ": [
    {
      "JOULES_CUR": 0,
      "timestamp": 1742385526
    },
    {
      "JOULES_CUR": 1,
      "timestamp": 1742385528
    },
    {
      "JOULES_CUR": 2,
      "timestamp": 1742385530
    },
    {
      "JOULES_CUR": 3,
      "timestamp": 1742385533
    },
    {
      "JOULES_CUR": 4,
      "timestamp": 1742385535
    }
  ],
  "LOCAL": {
    "pushState": {
      "id=23": {
        "measure": {
          "JOULES_CUR": 4,
          "timestamp": 1742385535
        },
        "measurement": 4,
        "measure_non_intialized": {
          "JOULES_CUR": 4,
          "timestamp": 1742385535
        }
      }
    }
  },
  "MEASUREMENTS_OBJ_NON": [
    {
      "JOULES_CUR": 4,
      "timestamp": 1742385535
    },
    {
      "JOULES_CUR": 4,
      "timestamp": 1742385535
    },
    {
      "JOULES_CUR": 4,
      "timestamp": 1742385535
    },
    {
      "JOULES_CUR": 4,
      "timestamp": 1742385535
    },
    {
      "JOULES_CUR": 4,
      "timestamp": 1742385535
    }
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions