Skip to content

card concurrent access race condition #11

@litinoveweedle

Description

@litinoveweedle

As discovered in the #10 (comment) and confirmed in the #10 (comment) , the card CLI (which is internally also used for the Python library) doesn't implement locking. This permits concurrent access to the card on I2C bus and potential garbage input states reporting. This manifest itself as cards inputs reported state constantly flapping, but only for the inputs which are currently set to high.

To reproduce this issue you can run multiple instances of the following script. The smaller is the sleep in the loop the higher is the chance for collision on the bus.

#!/bin/bash

declare -A INPUTS
INPUTS[1]=0
INPUTS[2]=0
INPUTS[3]=0
INPUTS[4]=0
INPUTS[5]=0
INPUTS[6]=0
INPUTS[7]=0
INPUTS[8]=0
INPUTS[9]=0
INPUTS[10]=0
INPUTS[11]=0
INPUTS[12]=0
INPUTS[13]=0
INPUTS[14]=0
INPUTS[15]=0
INPUTS[16]=0

declare -a KEYS
IFS=$'\n' KEYS=($(printf '%s\n' "${!INPUTS[@]}" | sort -n))


for ID in "${KEYS[@]}"
do
    INPUTS[${ID}]=$(./16inpind 3 optrd ${ID})
    echo "get initial input ${ID} : ${INPUTS[${ID}]}"
done
echo "--------------------------"

echo "start loop to detect input change"
while true
do
    for ID in "${KEYS[@]}"
    do
    	TMP=$(./16inpind 3 optrd ${ID})
    	if [ "${INPUTS[${ID}]}" -ne "${TMP}" ]
    	then
    	    echo "Input: ${ID} changed from: ${INPUTS[${ID}]} to: ${TMP}"
            INPUTS[${ID}]=${TMP}
    	fi
    done
    echo "-------"
    sleep 0.1
done

Based on the proposed fix I would like to ask for the thread safe implementation of the library (Python calling popen, and Node-RED using the exec node).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions