Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 2, 2025

Resolves issue #3190 where manual task execution fails with "Cannot write new event to database" error containing "unexpected end of JSON input".

Problem

When manually executing tasks through the UI or API, users encountered a 500 error with the message:

  • Frontend: "Request failed with status code 500"
  • Backend logs: level=error msg="unexpected end of JSON input" error="Cannot write new event to database"

Scheduled tasks worked correctly, but manual execution consistently failed.

Root Cause

The issue was in the populateTaskEnvironment() method in services/tasks/TaskRunner.go. The code attempted to unmarshal Environment.JSON without checking if the field was empty, causing a JSON parsing error when the environment template had no JSON content.

// This would fail if Environment.JSON was empty
err = json.Unmarshal([]byte(t.Environment.JSON), &tplEnvironment)

Solution

Added a validation check to only attempt JSON unmarshaling when the field contains data:

// Only unmarshal Environment.JSON if it's not empty
if t.Environment.JSON != "" {
    err = json.Unmarshal([]byte(t.Environment.JSON), &tplEnvironment)
    if err != nil {
        return
    }
}

This maintains the original functionality when valid JSON is present while preventing the parsing error for empty environment configurations.

Testing

  • Application builds and starts successfully
  • API endpoints respond correctly
  • No breaking changes to existing functionality
  • Compatible with both BoltDB and other database backends mentioned in the original issue

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@fiftin
Copy link
Collaborator

fiftin commented Sep 3, 2025

@copilot try again

- Add check for empty Environment.JSON before attempting to unmarshal
- Prevents "unexpected end of JSON input" error during manual task execution
- Fixes issue #3190 where manual tasks failed with environment JSON parsing errors

Co-authored-by: fiftin <[email protected]>
Copy link
Contributor Author

Copilot AI commented Sep 3, 2025

@copilot try again

I've identified and fixed the issue! The problem was in the populateTaskEnvironment() method where the code was attempting to unmarshal empty Environment.JSON without checking if it was valid first.

The fix adds a simple check to only unmarshal Environment.JSON if it's not empty, preventing the "unexpected end of JSON input" error during manual task execution.

Fixed in commit 48ddd21. The application now builds and runs successfully.

@Copilot Copilot AI changed the title [WIP] Problem: Cannot write new event to database Fix JSON unmarshaling error in task environment handling Sep 3, 2025
Copilot finished work on behalf of fiftin September 3, 2025 15:37
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.

2 participants