Issue50

Title Deadlock in lua scripts (2)
Priority bug Status resolved
Superseder Nosy List admin
Assigned To
Keywords Orthanc Core

Created on 2017-06-08.15:39:02 by admin, last changed by admin.

Messages
msg230 (view) Author: admin Date: 2017-06-08.15:39:02
[BitBucket user: Alain Mazy]
[BitBucket date: 2017-06-08.13:39:02]

note: this might be a duplicate of issue #25 (to be confirmed)

We have a lua scripts that modifies some instances.  Thousands of instances are processed correctly but, sometimes, the scripts enters a deadlock.

```
function OnStoredInstance(instanceId, tags, metadata, origin)

	-- Ignore the instances that result from a modification to avoid infinite loops
	if (metadata['ModifiedFrom'] == nil) then

		-- consider only the instances that comes from the Rest API (from a remote orthanc)
		if (origin['RequestOrigin'] == 'RestApi') then

			print('received an instance from ' .. origin['RemoteIp'])
			remoteIp = origin['RemoteIp']
			institutionName = tags['InstitutionName']
			
			if institutionName == 'XXX' then
				print('received an instance from XXX')  

				-- Send the modified instance to another modality
				SendToModality(instanceId, 'pacs')

			else -- YYY does not standardize its InstitutionName yet
				print('received an instance from YYY, updating InstitutionName')  

				institutionName = 'YYY'
				local modifyCommand = {}
				local replaceTags = {}
				replaceTags['InstitutionName'] = institutionName
				modifyCommand['Replace'] = replaceTags
				
				-- modify the instance (this will download the modified instance)
				local modifiedInstance = RestApiPost('/instances/' .. instanceId .. '/modify', DumpJson(modifyCommand, true))
				
				-- Upload the modified instance to the Orthanc database so that
				-- it can be sent by Orthanc to other modalities
				local modifiedInstanceId = ParseJson(RestApiPost('/instances/', modifiedInstance)) ['ID']

				-- Send the modified instance to another modality
				SendToModality(modifiedInstanceId, 'pacs')

				print('original id: ' .. instanceId .. ' - modified id: ' .. modifiedInstanceId)
				-- Delete the original instance but keep the modified one, it will be deleted by the Orthanc circular buffer
				RestApiDelete('/instances/' .. instanceId)
		    end
		
		end

	end

end
```

When getting stuck, it gets stuck between `print('received an instance from YYY, updating InstitutionName')` and `print('original id: ' .. instanceId .. ' - modified id: ' .. modifiedInstanceId)`
msg231 (view) Author: admin Date: 2018-05-22.13:13:05
[BitBucket user: Sébastien Jodogne]
[BitBucket date: 2018-05-22.11:13:05]

This issue should now be solved in branch "jobs". Please could you try and validate?
msg232 (view) Author: admin Date: 2019-01-25.14:15:44
[BitBucket user: Sébastien Jodogne]
[BitBucket date: 2019-01-25.13:15:44]

Unable to reproduce with the new jobs engine. This might have been solved by this refactoring.
History
Date User Action Args
2026-07-29 15:51:20admincreate