Skip to content

Fix saving of problem data (the persistence hash) when rendering fails. - #3288

Open
drgrice1 wants to merge 1 commit into
openwebwork:developfrom
drgrice1:bugfix/problem-data
Open

drgrice1 wants to merge 1 commit into
openwebwork:developfrom
drgrice1:bugfix/problem-data

Conversation

@drgrice1

@drgrice1 drgrice1 commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

When the PERSISTENCE_HASH is saved a fallback of '{}' is used inside the encode_json call if the PERSISTENCE_HASH key is undefined (or falsy). That encodes to the string "{}", and that string (literally including the quotes) is stored in the database. That is later decoded back to the string {} instead of the empty hash. In this case scaffolding problems throw an exception early in the pg post content processing phase that make it so that all parts of the scaffold are shown (with no scaffolding) and MathQuill answers are not injected, and all sorts of things fall apart. So this uses an anonymous hash instead as it should have. Furthermore, as a precaution when the WeBWorK::Utils::Rendering::constructPGOptions method decodes the data from the database, the JSON decoding step is performed in an eval and a fallback empty hash is used if the decoding fails (this will catch already stored invalid values).

This can happen, for instance, if a problem fails to render, but a student submits answers anyway. This actually happened in one of my classes.

There is further work to do in the case that a problem fails to render in general. We need to investigate what really should be saved in this case. This is the fail_render case from the WeBWorK::Utils::Rendering::renderPG method (an the corresponding GatewayQuiz handling of this) in which the problem completely fails and no PGcore object is even returned. In this case the current code saves the WeBWorK::Utils::ProblemProcessing::process_and_log_answer method saves some things from the PG state that are guaranteed to be undefined (such as the recorded_score, num_of_correct_ans, and num_of_incorrect_ans). So at the very least those should not be saved.

@drgrice1

Copy link
Copy Markdown
Member Author

I would like to make this a hotfix as well if no one objects to that.

@drgrice1

Copy link
Copy Markdown
Member Author

GitHub did something odd with the main conversation message for this pull request. It is fixed now.

@somiaj somiaj left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a hotfix is warranted.

@drgrice1
drgrice1 force-pushed the bugfix/problem-data branch from fa948f8 to 6cf5acd Compare November 17, 2026 19:31

@pstaabp pstaabp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exactly sure what to test here, but the code looks straightforward.

When the `PERSISTENCE_HASH` is saved a fallback of `'{}'` is used inside
the `encode_json` call if the `PERSISTENCE_HASH` key is undefined (or
falsy). That encodes to the string `"{}"`, and that string (literally
including the quotes) is stored in the database. That is later decoded
back to the string `{}` instead of the empty hash.  In this case
scaffolding problems throw an exception early in the pg post content
processing phase that make it so that all parts of the scaffold are
shown (with no scaffolding) and MathQuill answers are not injected, and
all sorts of things fall apart.  So this uses an anonymous hash instead
as it should have.  Furthermore, as a precaution when the
`WeBWorK::Utils::Rendering::constructPGOptions` method decodes the data
from the database, the JSON decoding step is performed in an `eval` and
a fallback empty hash is used if the decoding fails (this will catch
already stored invalid values).

This can happen, for instance, if a problem fails to render, but a
student submits answers anyway. This actually happened in one of my
classes.

There is further work to do in the case that a problem fails to render
in general. We need to investigate what really should be saved in this
case.  This is the `fail_render` case from the `WeBWorK::Utils::Rendering::renderPG`
method (an the corresponding `GatewayQuiz` handling of this) in which
the problem completely fails and no PGcore object is even returned.  In
this case the current code saves the `WeBWorK::Utils::ProblemProcessing::process_and_log_answer`
method saves some things from the PG state that are guaranteed to be
undefined (such as the `recorded_score`, `num_of_correct_ans`, and
`num_of_incorrect_ans`).  So at the very least those should not be
saved.
@drgrice1
drgrice1 force-pushed the bugfix/problem-data branch from 6cf5acd to 6a6f817 Compare November 17, 2026 23:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants