{"record":{"id":"75380e2f74a73863","repo":"instructure/canvas-lms","slug":"file-is-locked","errorCode":null,"errorMessage":"File is locked","messagePattern":"File is locked","errorType":"validation","errorClass":"ContentUnavailable","httpStatus":null,"severity":"error","filePath":"app/services/study_assist.rb","lineNumber":173,"sourceCode":"\n    def resolve_page(page_id)\n      page = @course.wiki_pages.not_deleted.find_by(url: page_id)\n      raise ContentUnavailable, \"Page not found\" if page.nil?\n      raise ContentUnavailable, \"Page access denied\" unless page.grants_right?(@user, :read)\n\n      shard_safe_key = shard_safe_cache_key_for(page)\n      text = Rails.cache.fetch(text_cache_key_for(:page, shard_safe_key), expires_in: TEXT_CACHE_TTL) do\n        html_to_text(page.body.to_s)\n      end\n\n      Content.new(kind: :page, id: page.id, cache_key_with_version: shard_safe_key, text:)\n    end\n\n    def resolve_file(file_id)\n      attachment = @course.attachments.find_by(id: file_id)\n      raise ContentUnavailable, \"File not found\" if attachment.nil? || attachment.deleted?\n      raise ContentUnavailable, \"File access denied\" unless attachment.grants_right?(@user, :read)\n      raise ContentUnavailable, \"File is locked\" if attachment.locked_for?(@user, check_policies: true)\n      raise UnsupportedContentType unless supported_attachment?(attachment)\n      raise ContentTooLarge, \"File exceeds #{MAX_FILE_BYTES} byte limit\" if attachment.size && attachment.size > MAX_FILE_BYTES\n\n      shard_safe_key = shard_safe_cache_key_for(attachment)\n      text = Rails.cache.fetch(text_cache_key_for(:file, shard_safe_key), expires_in: TEXT_CACHE_TTL) do\n        extract_attachment_text(attachment)\n      end\n\n      raise ContentUnavailable, \"No text available for file\" if text.blank?\n\n      Content.new(kind: :file, id: attachment.id, cache_key_with_version: shard_safe_key, text:)\n    end\n\n    def supported_attachment?(attachment)\n      return true if attachment.content_type&.start_with?(\"text/\")\n\n      ACCEPTED_FILE_MIMETYPES.include?(attachment.content_type)\n    end","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/instructure/canvas-lms/blob/1c9f0bb8013ed69c4f2efe11fd483025469b7e6c/app/services/study_assist.rb#L155-L191","documentation":"StudyAssist::ContentUnavailable with 'File is locked' is raised by resolve_file when attachment.locked_for?(@user, check_policies: true) returns true. Canvas files can be locked by module progression, availability windows, or master-course lock policies, blocking the student from the content.","triggerScenarios":"Calling #call with a readable but locked file: locked until a date, module completion requirement not met, sequential progress enforcement, or locked via course copy/master course policy.","commonSituations":"Students hitting Study Assist on files gated by module requirements; files with unlock-at dates in the future; blueprint/master course locked attachments; testing as a student vs teacher mismatch.","solutions":["Unlock the module requirement or availability date for the file","Test with a user who is not subject to the lock (e.g. teacher) to confirm lock policy is the cause","Rescue StudyAssist::ContentUnavailable and show the lock reason to the end user","Have students complete the prerequisite module items before using Study Assist on the file"],"exampleFix":"// before\nStudyAssist.new(course: @course, user: @user, prompt:, file_id: file_id).call\n// after\natt = @course.attachments.find(file_id)\nif att.locked_for?(@user, check_policies: true)\n  return render json: { error: 'file locked' }, status: :forbidden\nend\nStudyAssist.new(course: @course, user: @user, prompt:, file_id: file_id).call","handlingStrategy":"validation","validationCode":"att = course.attachments.find_by(id: file_id)\nreturn nil if att&.locked_for?(user, check_policies: true)","typeGuard":"null","tryCatchPattern":"begin\n  StudyAssist.new(course:, user:, prompt:, file_id:).call\nrescue StudyAssist::ContentUnavailable\n  render json: { error: 'file locked' }, status: :forbidden\nend","preventionTips":["Check locked_for?(user) before calling","Communicate module/lock prerequisites to users","Respect availability windows in UI file pickers"],"tags":["ruby","study-assist","locked-content","modules"],"backgroundTag":"permission-denied","analyzedSha":"1c9f0bb8013ed69c4f2efe11fd483025469b7e6c","analyzedAt":"2026-09-15T20:33:18.891Z","contentChangedAt":"2026-09-15T20:33:18.891Z","schemaVersion":2},"datasetVersion":"2026-09-23T02:17:17.105Z"}