Cannot serve site with shortcode containing resize_image

I’m in the process of switching my blog over from Jekyll but have got stuck trying to write a shortcode for displaying a resized image. The idea is that the shortcode should allow resizing the image but also link to the full size image using a link tag.

This is the image.html shortcode:

<figure class="image">
{% if height and width %}
{% set resized = resize_image(path=path, height=height, width=width, op=op) %}
{% elif width %}
{% set resized = resize_image(path=path, width=width, op="fit_width") %}
{% elif height %}
{% set resized = resize_image(path=path, height=height, op="fit_height") %}
{% endif %}
<a href="{{ path }}"><img src="{% if resized %}{{ resized | safe }}{% else %}{{ path }}{% endif %}"{%- if caption %} alt="{{ caption }}"{%- endif %} /></a>
{%- if caption -%}
    <figcaption>{{ caption }}</figcaption>
{%- endif -%}
</figure>

I am trying to use the shortcode for a post as follows:

{{ image(path="posts/postname/image.png", width=300, caption="Image caption.") }}

The index.md post and image.png image are in the directory content/posts/postname.

When I try serving the site it fails with the error:

Failed to build the site
Error: The process cannot access the file because it is being used by another process. (os error 32)

It occasionally seems to build but most of the time fails with the same error.

If I play around with the use of the resized variable inside the tag by adding or remove “| safe” I can sometimes get it to serve correctly and then everything looks as I would expect. The fact that it works occasionally makes me think there is some race condition on accessing the file and my changes to the shortcode sometimes cause the files to be updated in a different order that works.

Am I doing anything wrong in the shortcode or in the post markdown?

I’m running zola 0.11.0 on 64-bit Windows 10 Pro Version 2004 Build 19041.329.

I haven’t tested it yet but I think it might be because the shortcode in the post is also being displayed on my index page as part of the post summary. Will it be trying to resize the image as part of the index summary and part of the post?