import litesync as sqlite3
import json
import time
def connect_to_primary():
connection_string = "file:<path>/test.db?cipher=chacha20&key=<32bytekey>&node=secondary&connect=<server_ip>:<port>"
print("Attempting to connect to the database...")
try:
conn = sqlite3.connect(connection_string, uri=True)
print("Connection established.")
return conn
except sqlite3.Error as e:
print(f"Failed to connect to the database. Error: {e}")
return None
def check_database_ready(conn):
print("Checking if the database is ready...")
while True:
try:
cursor = conn.cursor()
cursor.execute("PRAGMA sync_status;")
result = cursor.fetchone()
if result:
sync_status = json.loads(result[0])
print(f"Sync status: {sync_status}")
if sync_status.get("db_is_ready", False):
print("Database is ready!")
return True
time.sleep(0.250)
except sqlite3.Error as e:
print(f"Error while checking database readiness: {e}")
return False
if __
name__ == "__main__":
conn = connect_to_primary()
if conn:
if check_database_ready(conn):
print("You can now use the database connection.")
# code to query data
time.sleep(1)
conn.close()
else:
print("Failed to establish a connection to the database.")
The above Python code snippet reproduces the issue. When I run the script for the first time to perform the initial sync and then manually kill the process to stop syncing halfway through, running the same code again results in a segmentation fault.
here is my code output
## ---------- First time run
Attempting to connect to the database...
Connection established.
Checking if the database is ready...
Sync status: {'use_litesync': True, 'node_type': 'secondary', 'node_id': 0, 'db_is_ready': False, 'sync_down_state': 'unknown', 'sync_up_state': 'unknown', 'last_tid': 0, 'log_storage': 'discard', 'peers': []}
Sync status: {'use_litesync': True, 'node_type': 'secondary', 'node_id': 0, 'db_is_ready': False, 'sync_down_state': 'unknown', 'sync_up_state': 'unknown', 'last_tid': 0, 'log_storage': 'discard', 'peers': []}
Sync status: {'use_litesync': True, 'node_type': 'secondary', 'node_id': 0, 'db_is_ready': False, 'sync_down_state': 'unknown', 'sync_up_state': 'unknown', 'last_tid': 0, 'log_storage': 'discard', 'peers': [{'node_type': 'unknown', 'node_id': 0, 'conn_type': 'outgoing', 'address': '<server_ip>:<port>', 'conn_state': 'connected', 'db_state': 'unknown', 'log_storage': 'unknown'}]}
Sync status: {'use_litesync': True, 'node_type': 'secondary', 'node_id': 0, 'db_is_ready': False, 'sync_down_state': 'unknown', 'sync_up_state': 'with local changes', 'last_tid': 0, 'log_storage': 'discard', 'peers': [{'node_type': 'primary', 'node_id': 1, 'conn_type': 'outgoing', 'address': '<server_ip>:<port>', 'conn_state': 'connected', 'db_state': 'unknown', 'log_storage': 'keep'}]}
Sync status: {'use_litesync': True, 'node_type': 'secondary', 'node_id': 83, 'db_is_ready': False, 'sync_down_state': 'downloading', 'download_progress': 0.0, 'sync_up_state': 'with local changes', 'last_tid': 0, 'log_storage': 'discard', 'peers': [{'node_type': 'primary', 'node_id': 1, 'conn_type': 'outgoing', 'address': '<server_ip>:<port>', 'conn_state': 'connected', 'db_state': 'unknown', 'log_storage': 'keep'}]}
Sync status: {'use_litesync': True, 'node_type': 'secondary', 'node_id': 83, 'db_is_ready': False, 'sync_down_state': 'downloading', 'download_progress': 1.260504, 'sync_up_state': 'with local changes', 'last_tid': 0, 'log_storage': 'discard', 'peers': [{'node_type': 'primary', 'node_id': 1, 'conn_type': 'outgoing', 'address': '<server_ip>:<port>', 'conn_state': 'connected', 'db_state': 'unknown', 'log_storage': 'keep'}]}
Sync status: {'use_litesync': True, 'node_type': 'secondary', 'node_id': 83, 'db_is_ready': False, 'sync_down_state': 'downloading', 'download_progress': 3.781513, 'sync_up_state': 'with local changes', 'last_tid': 0, 'log_storage': 'discard', 'peers': [{'node_type': 'primary', 'node_id': 1, 'conn_type': 'outgoing', 'address': '<server_ip>:<port>', 'conn_state': 'connected', 'db_state': 'unknown', 'log_storage': 'keep'}]}
Sync status: {'use_litesync': True, 'node_type': 'secondary', 'node_id': 83, 'db_is_ready': False, 'sync_down_state': 'downloading', 'download_progress': 4.201681, 'sync_up_state': 'with local changes', 'last_tid': 0, 'log_storage': 'discard', 'peers': [{'node_type': 'primary', 'node_id': 1, 'conn_type': 'outgoing', 'address': '<server_ip>:<port>', 'conn_state': 'connected', 'db_state': 'unknown', 'log_storage': 'keep'}]}
Sync status: {'use_litesync': True, 'node_type': 'secondary', 'node_id': 83, 'db_is_ready': False, 'sync_down_state': 'downloading', 'download_progress': 8.403361, 'sync_up_state': 'with local changes', 'last_tid': 0, 'log_storage': 'discard', 'peers': [{'node_type': 'primary', 'node_id': 1, 'conn_type': 'outgoing', 'address': '<server_ip>:<port>', 'conn_state': 'connected', 'db_state': 'unknown', 'log_storage': 'keep'}]}
Sync status: {'use_litesync': True, 'node_type': 'secondary', 'node_id': 83, 'db_is_ready': False, 'sync_down_state': 'downloading', 'download_progress': 12.605042, 'sync_up_state': 'with local changes', 'last_tid': 0, 'log_storage': 'discard', 'peers': [{'node_type': 'primary', 'node_id': 1, 'conn_type': 'outgoing', 'address': '<server_ip>:<port>', 'conn_state': 'connected', 'db_state': 'unknown', 'log_storage': 'keep'}]}
^CTraceback (most recent call last):
File "<path>/scripts/connect-to-primary.py", line 39, in <module>
if check_database_ready(conn):
File "<path>/scripts/connect-to-primary.py", line 30, in check_database_ready
time.sleep(0.250)
KeyboardInterrupt
# -- Second time run --
Attempting to connect to the database...
Connection established.
Checking if the database is ready...
Sync status: {'use_litesync': True, 'node_type': 'secondary', 'node_id': 83, 'db_is_ready': False, 'sync_down_state': 'unknown', 'sync_up_state': 'unknown', 'last_tid': 0, 'log_storage': 'discard', 'peers': []}
Sync status: {'use_litesync': True, 'node_type': 'secondary', 'node_id': 83, 'db_is_ready': False, 'sync_down_state': 'unknown', 'sync_up_state': 'unknown', 'last_tid': 0, 'log_storage': 'discard', 'peers': []}
Sync status: {'use_litesync': True, 'node_type': 'secondary', 'node_id': 83, 'db_is_ready': False, 'sync_down_state': 'unknown', 'sync_up_state': 'unknown', 'last_tid': 0, 'log_storage': 'discard', 'peers': [{'node_type': 'unknown', 'node_id': 0, 'conn_type': 'outgoing', 'address': '<server_ip>:<port>', 'conn_state': 'connected', 'db_state': 'unknown', 'log_storage': 'unknown'}]}
Segmentation fault (core dumped)