Service call failed: service [] responded with an error: error processing request: I/O operation on closed file
Hello,
I have a bunch of nodes running with ROS which call services from one another. Moreover I have each node write to a file general data to keep track of what's going on. I have one file for the main thread, and then 1 file for a callback function, and 1 file for the threads which handle service requests. For the latter I use a lock, so only one thread can write at a time. In general it seems to be working fine, however there are moments it breaks and gives me the following error: ValueError: I/O operation on closed file
. Somehow the file gets closed? I'm not sure what might be causing the issue or how to debug this? Any suggestions would be appreciated.
EDIT - code
#!/usr/bin/env python
--some imports
class Agent:
def __init__(self, delta, energy, ID, x, y, theta, red, green, blue, state, depend_nr, popSize, provaNr):
--declaration of some attributes
.........
self.lock = Lock()
filename = 'RESULT/pop_size.'+str(popSize) +'/prova.'+str(provaNr)+'/stdout_' + str(ID) + '_' + str(delta) +'_'+ str(depend_nr)
self.stdout_log = open(filename, 'w')
filename2 = 'RESULT/pop_size.'+str(popSize) +'/prova.'+str(provaNr)+'/stdout_callback' + str(ID) + '_' + str(delta) +'_'+ str(depend_nr)
self.stdout_log_callback = open(filename2, 'w')
filename3 = 'RESULT/pop_size.'+str(popSize) +'/prova.'+str(provaNr)+'/stdout_handle' + str(ID) + '_' + str(delta) +'_'+ str(depend_nr)
self.stdout_log_handle = open(filename3, 'w')
self.message = Message_Type()
self.message.id = self.about_me_wm.ID
self.message.rank = 10
self.message.group = 1
self.message.content = ''
for x in self.active_servs:
self.message.content = self.message.content + str(x[0]) + '|'
self.message.timestamp = time.strftime('%X', time.gmtime())
## ROS Topics and Services ################################################
###########################################################################
rospy.init_node('agent', anonymous=True)
myservice = '/agent' + str(ID) + '/serve'
srv = rospy.Service(myservice, Service_One, self.handle_serve)
self.publish_global = rospy.Publisher('msgs', Message_Type, queue_size=200)
rospy.Subscriber('trigger', Message_Type, self.callback)
rospy.sleep(10)
###########################################################################
## ROS Callbacks ##################################################################################
###################################################################################################
def handle_serve(self, request):
idx = -1
self.lock.acquire()
--modify some global vars
self.stdout_log_handle.write('[handle_serve ' + str(local_handle) + '] request.incoming: ' + request.incoming + '\n')
self.stdout_log_handle.write('[handle_serve ' + str(local_handle) + '] request.id: ' + str(request.id) + '\n')
print 'Receiving request from: %d, for task: %d \n Current client: %d' % (request.id, self.service_req[idx], self.current_client[idx])
self.stdout_log_handle.write('[handle_serve ' + str(local_handle) + '] ' + 'Receiving request from: %d, for task: %d. Current client: %d\n' % (request.id, self.service_req[idx], self.current_client[idx]))
timeout = time.time() + 30 # stop loop 20 sec from now
print 'service_resp: %s' % str(self.service_resp[idx])
self.stdout_log_handle.write('[handle_serve ' + str(local_handle) + '] service_resp: %s\n' % str(self.service_resp[idx]))
self.lock.release()
while not self.service_resp[idx]:
time.sleep(0.1)
if time.time() > timeout:
print 'timeout'
self.timeouts = self.timeouts + 1
self.service_resp_content[idx] = -1
self.lock.acquire()
self.stdout_log_handle.write('[handle_serve ' + str(local_handle) + '] timeout\n')
self.lock.release()
break
outgoing = str(self.service_resp_content[idx])
print 'request outgoing ' + outgoing
self.lock.acquire()
self.stdout_log_handle.write('[handle_serve ' + str(local_handle) + '] request outgoing ' + outgoing + '\n')
self.lock.release()
return outgoing
def call_serve(self, server, myid, request, anyone_index):
other_service = '/robot' + str ...
Example code would help.
You're right, I shall do that.