instructure/canvas-lms · error · ContentUnavailable

File is locked

Error message

File is locked

What it means

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.

Solutions

  1. Unlock the module requirement or availability date for the file
  2. Test with a user who is not subject to the lock (e.g. teacher) to confirm lock policy is the cause
  3. Rescue StudyAssist::ContentUnavailable and show the lock reason to the end user
  4. Have students complete the prerequisite module items before using Study Assist on the file

Example fix

// before
StudyAssist.new(course: @course, user: @user, prompt:, file_id: file_id).call
// after
att = @course.attachments.find(file_id)
if att.locked_for?(@user, check_policies: true)
  return render json: { error: 'file locked' }, status: :forbidden
end
StudyAssist.new(course: @course, user: @user, prompt:, file_id: file_id).call
Defensive patterns

Strategy: validation

Validate before calling

att = course.attachments.find_by(id: file_id)
return nil if att&.locked_for?(user, check_policies: true)

Type guard

null

Try / catch

begin
  StudyAssist.new(course:, user:, prompt:, file_id:).call
rescue StudyAssist::ContentUnavailable
  render json: { error: 'file locked' }, status: :forbidden
end

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.

Related errors


AI-assisted analysis of instructure/canvas-lms@1c9f0bb801 (2026-09-15). Data as JSON: /api/errors/75380e2f74a73863. Report an issue: GitHub.

Appendix: source

Thrown at app/services/study_assist.rb:173

    def resolve_page(page_id)
      page = @course.wiki_pages.not_deleted.find_by(url: page_id)
      raise ContentUnavailable, "Page not found" if page.nil?
      raise ContentUnavailable, "Page access denied" unless page.grants_right?(@user, :read)

      shard_safe_key = shard_safe_cache_key_for(page)
      text = Rails.cache.fetch(text_cache_key_for(:page, shard_safe_key), expires_in: TEXT_CACHE_TTL) do
        html_to_text(page.body.to_s)
      end

      Content.new(kind: :page, id: page.id, cache_key_with_version: shard_safe_key, text:)
    end

    def resolve_file(file_id)
      attachment = @course.attachments.find_by(id: file_id)
      raise ContentUnavailable, "File not found" if attachment.nil? || attachment.deleted?
      raise ContentUnavailable, "File access denied" unless attachment.grants_right?(@user, :read)
      raise ContentUnavailable, "File is locked" if attachment.locked_for?(@user, check_policies: true)
      raise UnsupportedContentType unless supported_attachment?(attachment)
      raise ContentTooLarge, "File exceeds #{MAX_FILE_BYTES} byte limit" if attachment.size && attachment.size > MAX_FILE_BYTES

      shard_safe_key = shard_safe_cache_key_for(attachment)
      text = Rails.cache.fetch(text_cache_key_for(:file, shard_safe_key), expires_in: TEXT_CACHE_TTL) do
        extract_attachment_text(attachment)
      end

      raise ContentUnavailable, "No text available for file" if text.blank?

      Content.new(kind: :file, id: attachment.id, cache_key_with_version: shard_safe_key, text:)
    end

    def supported_attachment?(attachment)
      return true if attachment.content_type&.start_with?("text/")

      ACCEPTED_FILE_MIMETYPES.include?(attachment.content_type)
    end

View on GitHub (pinned to 1c9f0bb801)