Skip to content

Commit

Permalink
Fixed random crash that was happening from time to time.
Browse files Browse the repository at this point in the history
Events were being fired inside the controller context closing the db in some executions making the end of the controller context close the db again and create a double free.
Made events wait until the end of the controllers context to be fired.
  • Loading branch information
drmargarido committed Mar 18, 2021
1 parent 3f995b9 commit e2124d4
Show file tree
Hide file tree
Showing 18 changed files with 116 additions and 58 deletions.
13 changes: 5 additions & 8 deletions lib/clock_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#include <pthread.h>
#include "clock.h"

#define SECOND 1000000
#define MINUTE (60 * SECOND)

lua_State *L;
unsigned int seconds = 0;
pthread_t thread;
int running = 0;

Expand All @@ -15,18 +17,13 @@ const char * TRIGGER_EVENT_CODE =

void * clock_proc(void *ptr){
while(running){
usleep(1000000);
seconds++;
if(seconds >= 60){
luaL_dostring(L, TRIGGER_EVENT_CODE);
seconds = 0;
}
usleep(MINUTE);
luaL_dostring(L, TRIGGER_EVENT_CODE);
}
}

void clock_init(lua_State * state){
L = state;
seconds = 0;
running = 1;
int result = pthread_create(&thread, NULL, clock_proc, (void*) NULL);
if(result != 0){
Expand Down
13 changes: 5 additions & 8 deletions lib/clock_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#include <stdio.h>
#include "clock.h"

#define SECOND 1000
#define MINUTE (60 * SECOND)

lua_State *L;
unsigned int seconds = 0;
HANDLE thread = NULL;
int running = 0;

Expand All @@ -14,19 +16,14 @@ const char * TRIGGER_EVENT_CODE =

DWORD WINAPI clock_proc(void* data) {
while(running){
Sleep(1000);
seconds++;
if(seconds >= 60){
luaL_dostring(L, TRIGGER_EVENT_CODE);
seconds = 0;
}
Sleep(MINUTE);
luaL_dostring(L, TRIGGER_EVENT_CODE);
}
return 0;
}

void clock_init(lua_State * state){
L = state;
seconds = 0;
running = 1;
thread = CreateThread(NULL, 0, clock_proc, NULL, 0, NULL);
if (!thread) {
Expand Down
18 changes: 11 additions & 7 deletions src/controller/add_task.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ local db_validators = require "src.validators.db_validators"
local decorators = require "src.decorators"
local use_db = decorators.use_db
local check_input = decorators.check_input
local use_events = decorators.use_events

-- Plugins
local event_manager = require "src.plugin_manager.event_manager"
local events = require "src.plugin_manager.events"

-- Controllers
Expand All @@ -23,7 +23,7 @@ return check_input(
{validators.is_text, validators.max_length(512), validators.min_length(1)},
{validators.is_text, validators.max_length(255), validators.min_length(1)},
},
use_db(function(db, description, project)
use_events(use_db(function(db, events_queue, description, project)
-- Create a new project if it doesn't exists
local project_exists, _ = db_validators.project_exists(project)
if not project_exists then
Expand Down Expand Up @@ -54,16 +54,20 @@ return check_input(
task_stmt:step()

local success, error = db_validators.operation_ok(db)
task_stmt:finalize()
if not success then
return false, error or "Failed to create the new task"
end

event_manager.fire_event(events.TASK_CREATED, {
description=description,
project=project,
date=current_date
table.insert(events_queue, {
id=events.TASK_CREATED,
data={
description=description,
project=project,
date=current_date
}
})

return true, nil
end)
end))
)
1 change: 1 addition & 0 deletions src/controller/autocomplete_project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ return check_input(
for row in query_stmt:nrows() do
table.insert(tasks, row.name)
end
query_stmt:finalize()

return tasks, nil
end)
Expand Down
1 change: 1 addition & 0 deletions src/controller/autocomplete_task.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ return check_input(
for row in query_stmt:nrows() do
table.insert(tasks, row.description)
end
query_stmt:finalize()

return tasks, nil
end)
Expand Down
15 changes: 10 additions & 5 deletions src/controller/create_project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,35 @@ local validators = require "src.validators.base_validators"
local db_validators = require "src.validators.db_validators"

-- Plugins
local event_manager = require "src.plugin_manager.event_manager"
local events = require "src.plugin_manager.events"

-- Decorators
local decorators = require "src.decorators"
local use_db = decorators.use_db
local check_input = decorators.check_input
local use_events = decorators.use_events

-- Create a new project without validating if it already exists
return check_input(
{
{validators.is_text, validators.max_length(255)}
},
use_db(function(db, project_name)
use_events(use_db(function(db, events_queue, project_name)
local sql_project = "INSERT INTO project (name) VALUES (?)"
local project_stmt = db:prepare(sql_project)
project_stmt:bind_values(project_name)
project_stmt:step()

if not db_validators.operation_ok(db) then
local success, _ = db_validators.operation_ok(db)
project_stmt:finalize()
if not success then
return false, "Failed to create the new project"
end

event_manager.fire_event(events.PROJECT_CREATED, {name=project_name})
table.insert(events_queue, {
id=events.PROJECT_CREATED,
data={name=project_name}
})
return true, nil
end)
end))
)
13 changes: 8 additions & 5 deletions src/controller/delete_task.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ local validators = require "src.validators.base_validators"
local db_validators = require "src.validators.db_validators"

-- Plugins
local event_manager = require "src.plugin_manager.event_manager"
local events = require "src.plugin_manager.events"

-- Decorators
local decorators = require "src.decorators"
local use_db = decorators.use_db
local check_input = decorators.check_input
local use_events = decorators.use_events

return check_input(
{
Expand All @@ -19,17 +19,20 @@ return check_input(
db_validators.task_exists
}
},
use_db(function(db, task_id)
use_events(use_db(function(db, events_queue, task_id)
local remove_query = "DELETE FROM task WHERE id=?"
local remove_stmt = db:prepare(remove_query)
remove_stmt:bind_values(task_id)
remove_stmt:step()

if not db_validators.operation_ok(db) then
local success, _ = db_validators.operation_ok(db)
remove_stmt:finalize()

if not success then
return false, "Failed to delete the task"
end

event_manager.fire_event(events.TASK_DELETE, {id=task_id})
table.insert(events_queue, {id=events.TASK_DELETE, data={id=task_id}})
return true, nil
end)
end))
)
17 changes: 12 additions & 5 deletions src/controller/edit_task_description.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ local validators = require "src.validators.base_validators"
local db_validators = require "src.validators.db_validators"

-- Plugins
local event_manager = require "src.plugin_manager.event_manager"
local events = require "src.plugin_manager.events"

-- Decorators
local decorators = require "src.decorators"
local use_db = decorators.use_db
local check_input = decorators.check_input
local use_events = decorators.use_events

return check_input(
{
Expand All @@ -23,7 +23,7 @@ return check_input(
validators.max_length(512)
}
},
use_db(function(db, task_id, new_value)
use_events(use_db(function(db, events_queue, task_id, new_value)
local end_edit = [[
UPDATE task SET description=? WHERE id=?
]]
Expand All @@ -33,11 +33,18 @@ return check_input(
task_id
)
end_stmt:step()
if not db_validators.operation_ok(db) then

local success, _ = db_validators.operation_ok(db)
end_stmt:finalize()

if not success then
return false, "Failed to edit the task description"
end

event_manager.fire_event(events.TASK_EDIT, {id=task_id, description=new_value})
table.insert(events_queue, {
id=events.TASK_EDIT,
data={id=task_id, description=new_value}
})
return true, nil
end)
end))
)
17 changes: 12 additions & 5 deletions src/controller/edit_task_project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ local db_validators = require "src.validators.db_validators"
local decorators = require "src.decorators"
local use_db = decorators.use_db
local check_input = decorators.check_input
local use_events = decorators.use_events

-- Plugins
local event_manager = require "src.plugin_manager.event_manager"
local events = require "src.plugin_manager.events"

-- Controllers
Expand All @@ -26,7 +26,7 @@ return check_input(
validators.max_length(255)
}
},
use_db(function(db, task_id, new_value)
use_events(use_db(function(db, events_queue, task_id, new_value)
local project_exists, _ = db_validators.project_exists(new_value)
if not project_exists then
local _, err = create_project(new_value)
Expand All @@ -46,11 +46,18 @@ return check_input(
task_id
)
project_stmt:step()
if not db_validators.operation_ok(db) then

local success, _ = db_validators.operation_ok(db)
project_stmt:finalize()

if not success then
return false, new_value.." - Failed to edit the task project"
end

event_manager.fire_event(events.TASK_EDIT, {id=task_id, project=new_value})
table.insert(events_queue, {
id=events.TASK_EDIT,
data={id=task_id, project=new_value}
})
return true, nil
end)
end))
)
24 changes: 15 additions & 9 deletions src/controller/edit_task_time.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ local db_validators = require "src.validators.db_validators"
local decorators = require "src.decorators"
local use_db = decorators.use_db
local check_input = decorators.check_input
local use_events = decorators.use_events

-- Plugins
local event_manager = require "src.plugin_manager.event_manager"
local events = require "src.plugin_manager.events"

-- Utils
Expand All @@ -26,7 +26,7 @@ return check_input(
{validators.is_date},
{}
},
use_db(function(db, task_id, start_time, end_time)
use_events(use_db(function(db, events_queue, task_id, start_time, end_time)
if end_time ~= nil then
if not validators.is_date(end_time) then
return false, "The end time must be a date or nil"
Expand Down Expand Up @@ -72,18 +72,24 @@ return check_input(
task_id
)
end

edit_stmt:step()
if not db_validators.operation_ok(db) then

local success, _ = db_validators.operation_ok(db)
edit_stmt:finalize()

if not success then
return false, "Failed to edit the time of the task"
end

event_manager.fire_event(events.TASK_EDIT, {
id = task_id,
start_time = start_time,
end_time = end_time
table.insert(events_queue, {
id=events.TASK_EDIT,
data={
id = task_id,
start_time = start_time,
end_time = end_time
}
})

return true, nil
end)
end))
)
1 change: 1 addition & 0 deletions src/controller/get_task.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ return check_input(
description=t.description
}, nil
end
task_stmt:finalize()

return nil, "Wanted task not found"
end)
Expand Down
1 change: 1 addition & 0 deletions src/controller/get_task_by_description.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ return check_input(
description=t.description
}, nil
end
task_stmt:finalize()

return nil, "Wanted task not found"
end)
Expand Down
1 change: 1 addition & 0 deletions src/controller/list_tasks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ return check_input(
description=row.description
})
end
query_stmt:finalize()

return tasks, nil
end)
Expand Down
1 change: 1 addition & 0 deletions src/controller/list_tasks_by_text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ return check_input(
description=row.description
})
end
query_stmt:finalize()

return tasks, nil
end)
Expand Down
5 changes: 4 additions & 1 deletion src/controller/set_task_in_progress.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ return check_input(
project_stmt:bind_values(task_id)
project_stmt:step()

if not db_validators.operation_ok(db) then
local success, _ = db_validators.operation_ok(db)
project_stmt:finalize()

if not success then
return false, "Failed to set the the task in progress"
end

Expand Down
Loading

0 comments on commit e2124d4

Please sign in to comment.