Write_output_integer_register command doesn't always sets the value

I’m using IronPython to send scripts and commands from C# to the robot through python rtde.py and socket.py modules.

I want to reset a few output registers such as output_int_register_24 and bits 0 and 1 of register output_bit_registers0_to_31. To do that I send the script below through socket

"sec clear_registers():\n" +
                    "    write_output_integer_register(24 ,0)\n" +
                    "    write_output_boolean_register(0, False)\n" +
                    "    write_output_boolean_register(1, False)\n" +
 "end\n";

After that, I want to make sure the registers have been reset. To do this, I have a loop that receives the state, checks the values of the registers output_int_register_24 and output_bit_registers0_to_31 for 0, and if they are really 0 then it breaks the loop. In most cases, the loop ends almost immediately, which means the registers are indeed set to 0. But sometimes the loop never stops, which means the registers are not set to 0. Meaning the condition

state.output_int_register_24 == 0 && state.output_bit_registers0_to_31 == 0

never met. I wonder what the problem might be.
In the case where this “infinite” loop occurs, if I disconnect and reconnect to the robot and send this script again, it sets them to 0.
Do I need to use different sets of commands in the script above?

rtde = rtde.RTDE(robotIP, rtdePort);
rtde.connect();

while (true)
{
	var state = rtde.receive(false);
	if (state == null)
		throw new RTDEException("Couldn't recieve data package from RTDE");

	if (state.output_int_register_24 == 0 && state.output_bit_registers0_to_31 == 0)
		break;
}