From caf9901c1bcdcf97e0034a01b4a619e2eb1e5bc3 Mon Sep 17 00:00:00 2001 From: Joel Barciauskas <joel@edx.org> Date: Fri, 9 Dec 2016 15:04:29 -0500 Subject: [PATCH] [PLAT-1109] Suppress IntegrityErrors caused by race condition Suppress IntegrityErrors that may be caused by a race condition between processes resulting from a slow-loading page and a user refreshing the page. --- .../courseware/user_state_client.py | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/lms/djangoapps/courseware/user_state_client.py b/lms/djangoapps/courseware/user_state_client.py index 02316fec344..ecf6647613a 100644 --- a/lms/djangoapps/courseware/user_state_client.py +++ b/lms/djangoapps/courseware/user_state_client.py @@ -271,15 +271,25 @@ class DjangoXBlockUserStateClient(XBlockUserStateClient): evt_time = time() for usage_key, state in block_keys_to_state.items(): - student_module, created = StudentModule.objects.get_or_create( - student=user, - course_id=usage_key.course_key, - module_state_key=usage_key, - defaults={ - 'state': json.dumps(state), - 'module_type': usage_key.block_type, - }, - ) + try: + student_module, created = StudentModule.objects.get_or_create( + student=user, + course_id=usage_key.course_key, + module_state_key=usage_key, + defaults={ + 'state': json.dumps(state), + 'module_type': usage_key.block_type, + }, + ) + except IntegrityError: + # PLAT-1109 - Until we switch to read committed, we cannot rely + # on get_or_create to be able to see rows created in another + # process. This seems to happen frequently, and ignoring it is the + # best course of action for now + log.warning("set_many: IntegrityError for student {} - course_id {} - usage key {}".format( + user, repr(unicode(usage_key.course_key)), usage_key + )) + return num_fields_before = num_fields_after = num_new_fields_set = len(state) num_fields_updated = 0 -- GitLab