Skip to content

Commit

Permalink
Merge pull request #4500 from dodona-edu/feature/check-memory-lower-l…
Browse files Browse the repository at this point in the history
…imit

Check lower limit for memory_limit setting
  • Loading branch information
chvp authored Mar 21, 2023
2 parents 51f29e7 + 6bce404 commit 7669014
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
25 changes: 17 additions & 8 deletions app/models/exercise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,23 @@ def last_submission!(user, deadline = nil, course = nil)

def check_memory_limit
return unless ok?
return unless merged_config.fetch('evaluation', {}).fetch('memory_limit', 0) > 500_000_000 # 500MB

c = config
c['evaluation'] ||= {}
c['evaluation']['memory_limit'] = 500_000_000
store_config(c, "lowered memory limit for #{name}\n\nThe workers running the student's code only have 4 GB of memory " \
"and can run 6 students' code at the same time. The maximum memory limit is 500 MB so that if 6 students submit " \
'bad code at the same time, there is still 1 GB of memory left for Dodona itself and the operating system.')

limit = merged_config.fetch('evaluation', {}).fetch('memory_limit', nil)
return if limit.nil?

if limit > 500_000_000 # 500MB
c = config
c['evaluation'] ||= {}
c['evaluation']['memory_limit'] = 500_000_000
store_config(c, "lowered memory limit for #{name}\n\nThe workers running the student's code only have 4 GB of memory " \
"and can run 6 students' code at the same time. The maximum memory limit is 500 MB so that if 6 students submit " \
'bad code at the same time, there is still 1 GB of memory left for Dodona itself and the operating system.')
elsif limit < 10_000_000 # 10 MB
c = config
c['evaluation'] ||= {}
c['evaluation']['memory_limit'] = 10_000_000 # 10 MB
store_config(c, "raised memory limit for #{name}\n\nMemory limits under 10MB are not allowed.")
end
end

def self.move_relations(from, to)
Expand Down
10 changes: 10 additions & 0 deletions test/models/repository_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,16 @@ def teardown
assert_equal 500_000_000, JSON.parse(File.read(File.join(@remote.path, @echo.path, 'config.json')))['evaluation']['memory_limit']
end

test 'should overwrite memory limit that is too low' do
@remote.update_json("#{@echo.path}/config.json", 'set a very low memory limit') do |json|
json['evaluation']['memory_limit'] = 10
json
end
@repository.reset
@repository.process_activities
assert_equal 10_000_000, JSON.parse(File.read(File.join(@remote.path, @echo.path, 'config.json')))['evaluation']['memory_limit']
end

test 'should convert to content page' do
assert @echo.submissions.empty?
@remote.update_json("#{@echo.path}/config.json", 'convert to content page') do |json|
Expand Down
7 changes: 2 additions & 5 deletions test/testhelpers/remote_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ def update_json(rel_path, msg = nil)
end

def update_file(rel_path, msg = nil)
File.open(File.join(@path, rel_path), 'r+') do |f|
contents = f.read
f.seek(0, IO::SEEK_SET)
f.write(yield contents)
end
contents = File.read(File.join(@path, rel_path))
File.write(File.join(@path, rel_path), (yield contents))
msg ||= "update #{rel_path}"
commit msg
end
Expand Down

0 comments on commit 7669014

Please sign in to comment.